JavaScript Program to Count rotations required to sort given array in non-increasing order

We will be writing a program to count the number of rotations required to sort an array in non-increasing order. A rotation moves the first element to the end of the array. The program identifies where the array starts to break the non-increasing pattern and calculates the minimum rotations needed.

Understanding the Problem

For an array to be sorted in non-increasing order, each element should be greater than or equal to the next element. If we have a rotated sorted array, we need to find the rotation point and calculate how many positions we need to rotate to restore the non-increasing order.

Approach

The approach to counting rotations required to sort an array in non-increasing order is as follows:

  • Find the rotation point where the array breaks the non-increasing pattern.

  • Traverse the array from left to right to find where a smaller element appears after a larger one.

  • The index of this break point indicates the number of rotations needed.

  • If no break point is found, the array is already sorted in non-increasing order.

Method 1: Finding the Rotation Point

This method finds where the non-increasing order is broken:

function countRotationsNonIncreasing(arr) {
   let n = arr.length;
   
   // Find the rotation point
   for (let i = 0; i < n - 1; i++) {
      // If current element is smaller than next, we found the rotation point
      if (arr[i] < arr[i + 1]) {
         return n - i - 1;
      }
   }
   
   // Array is already sorted in non-increasing order
   return 0;
}

let arr1 = [6, 12, 15, 18, 2, 3];
let arr2 = [18, 15, 12, 6, 3, 2];
let arr3 = [3, 6, 12, 15, 18, 2];

console.log("Array:", arr1);
console.log("Rotations needed:", countRotationsNonIncreasing(arr1));

console.log("\nArray:", arr2);
console.log("Rotations needed:", countRotationsNonIncreasing(arr2));

console.log("\nArray:", arr3);
console.log("Rotations needed:", countRotationsNonIncreasing(arr3));
Array: [ 6, 12, 15, 18, 2, 3 ]
Rotations needed: 2

Array: [ 18, 15, 12, 6, 3, 2 ]
Rotations needed: 0

Array: [ 3, 6, 12, 15, 18, 2 ]
Rotations needed: 1

Method 2: Finding Maximum Element Position

This alternative approach finds the maximum element and calculates rotations based on its position:

function countRotationsUsingMax(arr) {
   let n = arr.length;
   let maxIndex = 0;
   
   // Find the index of maximum element
   for (let i = 1; i < n; i++) {
      if (arr[i] > arr[maxIndex]) {
         maxIndex = i;
      }
   }
   
   // Check if array is already sorted in non-increasing order
   let isSorted = true;
   for (let i = 0; i < n - 1; i++) {
      if (arr[i] < arr[i + 1]) {
         isSorted = false;
         break;
      }
   }
   
   return isSorted ? 0 : n - maxIndex - 1;
}

let testArray = [8, 9, 10, 1, 2, 3, 4, 5];
console.log("Array:", testArray);
console.log("Rotations needed:", countRotationsUsingMax(testArray));
Array: [ 8, 9, 10, 1, 2, 3, 4, 5 ]
Rotations needed: 5

Visualization Example

Let's trace through how the rotation works:

function demonstrateRotations(arr) {
   console.log("Original array:", arr);
   
   let rotations = countRotationsNonIncreasing(arr.slice());
   console.log("Rotations needed:", rotations);
   
   // Show the rotation process
   let current = arr.slice();
   for (let i = 0; i < rotations; i++) {
      // Rotate right: move last element to front
      let temp = current.pop();
      current.unshift(temp);
      console.log(`After rotation ${i + 1}:`, current);
   }
   
   return current;
}

let example = [4, 5, 6, 1, 2, 3];
let sorted = demonstrateRotations(example);
console.log("Final non-increasing check:", sorted.every((val, i) => i === 0 || sorted[i-1] >= val));
Original array: [ 4, 5, 6, 1, 2, 3 ]
Rotations needed: 3
After rotation 1: [ 3, 4, 5, 6, 1, 2 ]
After rotation 2: [ 2, 3, 4, 5, 6, 1 ]
After rotation 3: [ 1, 2, 3, 4, 5, 6 ]
Final non-increasing check: false

Comparison of Methods

Method Time Complexity Space Complexity Best For
Rotation Point Finding O(n) O(1) General cases
Maximum Element Position O(n) O(1) When maximum is clearly defined

Key Points

  • The rotation count depends on finding where the sorted order is broken.

  • Right rotation moves elements from end to beginning.

  • An already sorted non-increasing array requires 0 rotations.

  • The algorithm works for arrays that were originally sorted and then rotated.

Conclusion

Counting rotations to sort an array in non-increasing order involves finding the rotation point where the order breaks. Both methods provide O(n) time complexity solutions for efficiently determining the minimum rotations needed.

Updated on: 2026-03-15T23:19:01+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements