Home > Web Front-end > JS Tutorial > body text

JavaScript program to check if all rotations of a given number are greater than or equal to a given number

WBOY
Release: 2023-09-08 17:49:08
forward
1455 people have browsed it

JavaScript 程序检查给定数字的所有旋转是否大于或等于给定数字

In this article, we will go through a JavaScript program to check if all the rotations of a given number are greater than or equal to a given number. We'll write an algorithm and explain each step we're doing. The time complexity of the codes that will be discussed will be optimistic, while the space complexity will improve from one code to the other.

Problem Introduction

In this problem we are given a number and we have to check if every rotation is greater than the current number or simply we have to find the rotation of the current number that is less than the current number and if there is a smaller number in the rotation then we will return false, otherwise we will return true.

Rotation of given number

Example

The rotation of a given number can be of two types: clockwise or counterclockwise. In clockwise rotation, we take the last digit from the number and add it before the first digit. For example -

var number = 1234 
var last_digit = number%10;
number /= 10;
number = Math.floor(number)
var answer = last_digit.toString() + number.toString();
console.log("The first rotation of the given number is: " + answer)
Copy after login

In the above code, we are given a number and we have to find the first rotation of the given number. First, we store the last digit of the current number in another variable and then remove the last digit of the given number by dividing it by 10 and taking the bound.

Finally, we append the current number after the last number, which gives us the first spin.

Example

To get the next spin or the second spin of the current number, we can get the next spin of the first spin or even we can do it by another method that is common to any spin, let's do it by code have a look-

var number = 1234 
var i = 2
var n_string = number.toString()
var last_i_elements = n_string.substring(n_string.length-i);
var answer = last_i_elements+ n_string.substring(0,n_string.length-i);
console.log("The ith rotation of the given number is: " + answer)
Copy after login

In the above code, we are given a number and we have to find the ith rotation of the given number. First, we store the last "i" digit of the current number in another variable.

Finally, we append the current number to the string containing the last "i" number, which gives us the first spin.

The method discussed above is to rotate the number clockwise, for counterclockwise rotation we have to select the number from the front and append it to the end.

When there is no specification of which rotation we must choose, we choose clockwise. So in the example we will see a clockwise rotation.

method

This method is a brute force method, in this method we will find every spin of a given number and check every higher number. If we find any number less than the current number then we will return false otherwise return true.

Example

First let's take a look at the code, then we will move to the explanation of the code -

function check(number,i){
   var n_string = number.toString()
   var last_i_elements = n_string.substring(n_string.length-i);
   var answer = last_i_elements+ n_string.substring(0,n_string.length-i);
   if(answer < n_string){
      return false;
   }
   return true;
}

var number = 12345
// checking for every rotation
var ans = true;
for(var i=1;i
Copy after login

In the above program, first we iterate from 1 to the size of the number minus 1 in a for loop to get each rotation from 1 to the size minus 1. In each iteration, we call a predefined function.

In the function we will get the ith rotation which is passed as argument to the function and compared with the given number. If the ith rotation is less than the given number, then we will return false as return value, else return true.

We maintain a variable called answer which will store the true and false values ​​returned from the function and print the answer as required.

Time and space complexity

In the above code, we call the check() function n times in total, where n is the size of the given number. Inside the function, we create a substring that is a copy of the given number and iterate n times, meaning we use it n*n times. Therefore, the time complexity of the given function is O(N*N).

In the check function, every time we create a copy of the given number, it means we are using extra N spaces. Therefore, the space complexity of the given function is O(N).

general idea

In the above code, if all the numbers are different, then we can get the answer in just O(N) time complexity and O(1) space complexity, because if any number is less than the first number, then Meaning there is a number that may be less in an exact spin compared to the initial number.

in conclusion

In this tutorial, we use a JavaScript program to check if all rotations of a given number are greater than or equal to a given number. The time complexity of the program is O(N*N) and the space complexity is O(N), where N is the size of the given number. We have implemented a program where we can find every spin of a given number and compare it with the original number.

The above is the detailed content of JavaScript program to check if all rotations of a given number are greater than or equal to a given number. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!