JavaScript Program to Check whether all the rotations of a given number are greater than or equal to the given number or not


In this article, we will go through a JavaScript program to check whether all the rotations of a given number are greater than or equal to the given number or not. We will write an algorithm and explain every step that what we are doing. The time complexity of the code that is going to discuss will be optimistic and space complexity will all be improved from one code to another.

Introduction to Problem

In the problem, we are given a number and we have to check for each rotation whether they all are greater than the current number or not or simply we have to find a rotation of the current number that is smaller than the current number if the smaller number is present in the rotation then we will return false otherwise we will return true.

Rotations of Given Number

Example

Rotation of the given number can be of two types clockwise or anti-clockwise. In the clockwise rotation, we take the last digit from the number and add it before the first number. 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)

In the above code, we have given a number and we have to find the first rotation of the given number. First, we stored the last digit of the current number in another variable and then removed the last digit from the given number by dividing it by 10 and taking the floor.

In the end, we have appended the current number after the last digit which gave us the first rotation.

Example

To get the next rotation or the second rotation for the current number we can get the next rotation of the first rotation or even we can go for that by another method that is universal for any rotation, let’s see it through the code −

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)

In the above code, we have given a number and we have to find the ith rotation of the given number. First, we stored the last ‘i’ digits of the current number in another variable.

In the end, we have appended the current number after the string containing the last ‘i’ digits which gave us the first rotation.

The above-discussed methods are for the rotation of the number in a clockwise manner and in an anti-clockwise way we have to pick the number from the front and append that at the last.

When it is not specified which way to rotation we have to pick then we go for the clockwise manner. So, in the example, we will see the rotation in a clockwise manner.

Approach

This approach is the brute force approach and in this method, we are going to find every rotation of the given number and check for each number that is greater. If we find any number smaller than the current number then we will return false otherwise true.

Example

First, let’s see 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<number.toString().length;i++){
   ans = check(number,i);
   if(ans == false){
      break;
   }
}
if(ans == true){
   console.log("There is no rotation present which is less then given number")
}
else{
   console.log("There is a rotation of given number present which is less then given number")
}

In the above program, first we have iterated in a for loop from 1 to size of number minus one to get every rotation from 1 to size minus 1. In each iteration we have called a pre-defined function.

In the function, we are going to get the ith rotation which is passed as the parameter to the function and compare with the given number. If the ith rotation is less as compare to the given number then we will return false as return value otherwise true.

We have maintained a variable named answer which will stored the true and false value returned from the function and will print the answer as per the requirement.

Time and Space Complexity

In the above code, we have called the check() function total number of n times where n is the size of the given number. Inside the function we have created a substring that is copy of the given number and takes the n iteration which means we are using the n*n time. So, the time complexity of the given function is O(N*N).

In the check function every time we are creating a copy of the given number which means we are using the extra of the N space. Hence, space complexity of the given function is the O(N).

General Idea

In the above code, if all the number are distinct then we can get the answer in just O(N) time complexity and O(1) space complexity because if any digit is smaller than the first digit that means there is a chance that in exact one rotation the number will be less as compare to the initial number.

Conclusion

In this tutorial, we have gone through a JavaScript program to check whether all the rotations of a given number are greater than or equal to the given number or not. Time complexity of the program is O(N*N) and space complexity of the program is O(N) where N is the size of the given number. We have implemented a program where we found every rotation of the given number and compared with the original number.

Updated on: 24-Mar-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements