Open In App

Find the Rotation Count in Rotated Sorted array in JavaScript

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Rotated Sorted Array is an array type in JavaScript that has been rotated to the left or right by some random number of positions. In this article, we will understand how to find the count of rotations performed on the original sorted array to obtain the given sorted array using JavaScript language. In this article, we will mainly cover three different approaches.

There are different approaches for finding the rotation count in a rotated sorted array in JavaScript. Let’s discuss each of them one by one.

  • Using Recursive Approach
  • Using Modulo Operation Approach
  • Repeated Concatenation and Substring Approach

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Recursive Approach

In this approach, we will develop the recursive function that will give us the Rotation count. When we call the recursive function, it basically will check the middle element is the minimum element. If not, then it will search for the left and right parts of the array and then it will find the desired rotation count.

Syntax:

function functionName(parameters) {
// Base case: The condition where the recursion ends.
if (baseCaseCondition) {
// Return the base case value.
return baseCaseValue;
} else {
// Recursive call: Call the function with updated parameters.
return functionName(updatedParameters);
}
}

Example: In this example, we will find the rotation count in a rotated sorted array using the Recursive Approach.

Javascript




function findRotationCount(arr_input, low_part, high_part) {
    if (low_part >= high_part)
        return 0;
  
    if (arr_input[low_part] <= arr_input[high_part])
        return low_part;
  
    let mid_part =
        Math.floor((low_part + high_part) / 2);
    if (arr_input[mid_part] < arr_input[mid_part - 1]
        && arr_input[mid_part] < arr_input[mid_part + 1])
        return mid_part;
  
    if (arr_input[high_part] > arr_input[mid_part])
        return findRotationCount(
            arr_input, low_part, mid_part - 1);
  
    else
        return findRotationCount(
            arr_input, mid_part + 1, high_part);
}
let inputArray = [4, 5, 6, 7, 1, 2, 3];
let output =
    findRotationCount(
        inputArray, 0, inputArray.length - 1);
console.log("Rotation Count is:", output);


Output

Rotation Count is: 4

Approach 2: Using Modulo Operation Approach

In this approach, basically, we will use the modulo operation on the input-rotated array with respect to the 1st element of our array. The index where our modulo operation output becomes less will represent the rotation count.

Syntax:

a%b

Example: In this example, we will find the rotation count in a rotated sorted array using the Modulo Operation.

Javascript




function rotationCountUsingModuloOperation(arrInput) {
    let tempVar = arrInput.length;
    let minimumIndex = 0;
    let minimumValue = arrInput[0];
    for (let i = 1; i < tempVar; i++) {
        if (arrInput[i] < minimumValue) {
            minimumValue = arrInput[i];
            minimumIndex = i;
        }
    }
    var rotationCountValue = minimumIndex % tempVar;
    return rotationCountValue;
}
let inputArr = [6, 7, 8, 1, 2, 3, 4, 5]
let output = rotationCountUsingModuloOperation(inputArr);
console.log("Rotation Count is:", output);


Output

Rotation Count is: 3

Approach 3: Using Repeated Concatenation and Substring

In this approach first of all we will concatenate our inputted or the original array with itself. Then we will find the sorted substring that is equal to the original array length. After doing this, starting index of the sorted substring which is repeated will consists of the rotation count.

Syntax:

function rotationCountUsingConcat(arrInput) {
const dupArray =
arrInput.concat(arrInput);
const sortedSubstr =
dupArray.slice().sort((a, b) => a - b);
const rotCountVal =
dupArray.indexOf(sortedSubstr[0]);
return rotCountVal;
}

Example: In this example, the rotationCountUsingConcat function calculates the rotation count of a circularly rotated array using concatenation, sorting, and index finding.

Javascript




function rotationCountUsingConcat(arrInput) {
    const dupArray =
        arrInput.concat(arrInput);
    const sortedSubstr =
        dupArray.slice().sort((a, b) => a - b);
    const rotCountVal =
        dupArray.indexOf(sortedSubstr[0]);
    return rotCountVal;
}
const inputArr =
    [15, 18, 2, 3, 6, 12];
const output =
    rotationCountUsingConcat(inputArr);
console.log("Rotation Count is:", output);


Output

Rotation Count is: 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads