JavaScript Program to Check if it is possible to sort the array after rotating it


Rotating an array means moving the elements of each index (excluding one end ) to the following index for the right rotation and the previous index for the left rotation. For the right rotation zeroth index takes the value of the last index and vice-versa for the left rotation. Sort an array means that all the elements are in the proper increasing order. We will implement the proper code with an explanation and time, and space complexity discussion.

Example

Input: 5 6 9 10 2 3 3 4
Output: Yes

Explanation: We can rotate the given array 4 times to get the sorted array.

First Rotation: 4 5 6 9 10 2 3 3
Second Rotation: 3 4 5 6 9 10 2 3
Third Rotation: 3 3 4 5 6 9 10 2
Fourth Rotation: 2 3 3 4 5 6 9 10

Naive Approach

In this approach, the main idea is that after the number of rotations equals to the length of the array then we will get the same array. So, we will rotate the array equal to its length a number of times and for each rotation, we will use the two-pointer and swapping method. At each rotation, we will check whether the current array is sorted or not.

Example

// function to rotate the given array
function rotate(arr){
   var l = 0;
   var r = arr.length-1;
   while(l < r){
      arr[l] += arr[r];
      arr[r] = arr[l]-arr[r];
      arr[l] = arr[l]-arr[r];
      l++;
   }
   return arr;
}

// function to check if the given array is increasing or not
function increasing(arr){

   // getting the size of array
   var len = arr.length
   
   // traversing over the array
   for(var i = 1; i < len; i++){
      if(arr[i] < arr[i-1]){
         return false;
      }
   }
   return true;
}

// function to check whether the given array can become increasing or decreasing after certain rotations
function check(arr){
   var k = arr.length
   while(k--){
      if(increasing(arr)){
         console.log("The given array can be sorted after " + (arr.length-k-1) + " rotations");
         return
      }
      arr = rotate(arr);
   }
   console.log("The given array cannot be sorted");
}

//defining the given array
var arr = [5, 6, 9, 10, 2, 3, 3, 4]
console.log("Given array is: " + arr);

// calling the function
check(arr);

Output

Given array is: 5,6,9,10,2,3,3,4
The given array can be sorted after 4 rotations

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the size of the array. We are rotating the array N times and each rotation takes N move.

The space complexity of the above code is O(1), as we are not using any extra space.

Efficient Approach

The time complexity of the previous code is high which can be lowered by an observation that if the given array is sorted or is sorted in two parts, the elements of the second part are less than or equal as compared to the first half and both the halves are sorted in itself.

Example

// function to check if the given array is increasing or not
function increasing(arr){

   // getting the size of the array
   var len = arr.length
   
   // traversing over the array
   var i = 0;
   for(var i = 1; i < len; i++){
      if(arr[i] < arr[i-1]){
         break;
      }
   }
   if(i == len) return true;
   i++;
   for(; i< len; i++){
      if(arr[i] < arr[i-1]){
         return false;
      }
   }
   return arr[len-1] <= arr[0];
}

// function to check whether the given array can become increasing or decreasing after certain rotations
function check(arr){
   if(increasing(arr)){
      console.log("The given array can be sorted after ");
   }
   else{
      console.log("The given array cannot be sorted");
   }
}

//defining the given array
var arr = [5, 4, 9, 10, 2, 3, 3, 4]
console.log("Given array is: " + arr);

// calling the function
check(arr);

Output

Given array is: 5,4,9,10,2,3,3,4
The given array cannot be sorted

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the array. We are traversing over the array only once.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript code to check whether we can sort the elements by rotating its elements. Rotating an array means moving the elements of each index (excluding one end ) to the following index for the right rotation and the previous index for the left rotation. We have implemented two approaches one with a time complexity of O(N*N) and the other with O(N).

Updated on: 13-Apr-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements