- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript Program for Check if an array is sorted and rotated

A sorted array is an array where all the elements are present in increasing order. We have given an array of size N and an array containing the distinct integers (which means every integer is present only one time). We have to check the array is sorted and rotated in a clockwise direction. Here we have to return ‘YES’ if the array is sorted and rotated otherwise, we have to return ‘NO’.

**Note** − Here we are talking about the sorted and rotated means at least one rotation should be present. We cannot consider a sorted array as a sorted and rotated array.

Let us assume we have given an array of size N as

### Input

N = 5 Array = [5, 1, 2, 3, 4]

### Output

Yes, the given array is sorted and rotated.

### Explanation

An array is a sorted array and rotated by 1 place

Sorted array = [1, 2, 3, 4, 5] Rotated above sorted array by 1 place, we get = [5, 1, 2, 3, 4]

Sorted and rotated array by 1 place matches the input array so the output is, Yes.

### Input

N = 6 Array = [6, 5, 1, 2, 3, 4]

### Output

No, the given array is not sorted and rotated array.

### Explanation

The given array is a not sorted and rotated array

Sorted array = [1, 2, 3, 4, 5, 6] Rotated above sorted array by 1 place, we get = [6, 1, 2, 3, 4, 5] Rotated above sorted array by 2 place, we get = [5, 6, 1, 2, 3, 4] Rotated above sorted array by 3 place, we get = [4, 5, 6, 1, 2, 3] Rotated above sorted array by 4 place, we get = [3, 4, 5, 6, 1, 2] Rotated above sorted array by 5 place, we get = [2, 3, 4, 5, 6, 1]

None of the above sorted and rotated arrays is matched to the input array so the answer is, No.

## Approach

Here we are going to discuss two approaches. Let’s see them below section −

### Approach 1: Check if an Array is Sorted and Rotated by Finding a Pivot Element (Minimum Number)

The idea of this approach is that we are going to find the minimum number and if our array is sorted and rotated the values before and after the minimum number should be in a sorted manner.

### Example

// function to check if the given array is sorted and rotated function check(arr){ var len = arr.length; var mi = Number.MAX_VALUE; // variable to find the smallest number var idx_min = -1; // variable to store the index of smallest number; // traversing over the array to find the minimum element and its index for(var i = 0; i < len; i++){ if (arr[i] < mi){ mi = arr[i]; idx_min = i; } } // traversing over the array to find that all the elements // before the minimum element are sorted for(var i = 1; i < idx_min; i++){ if (arr[i] < arr[i - 1]){ return false; } } // traversing over the array to find that all the elements after the minimum element are sorted for(var i = idx_min + 1; i < len; i++){ if (arr[i] < arr[i - 1]){ return false; } } // checking if the last element of the array is smaller than the first element or not if(arr[len-1] > arr[0]){ return false; } else{ return true; } } // defining the array var arr = [5, 1, 2, 3, 4]; console.log("The given array is: ") console.log(arr); // calling to the function if(check(arr)){ console.log("Yes, the given array is sorted and rotated"); } else{ console.log("No, the given array is not sorted and rotated array") }

Time Complexity − O(N), where N is the size of the array.

Space Complexity − O(1), because we are not using any extra space.

### Approach 2: Check if an Array is Sorted and Rotated by Counting Adjacent Inversion

The idea of this approach is that we are going to traverse the array and check if the previous element is less than the current element. For the sorted and rotated array to count if the previous element is greater than the current element must be 1, otherwise, the array is not sorted and rotated.

### Example

// function to check if the given array is sorted and rotated function check(arr){ var len = arr.length; var count = 0; // variable to count the adjacent inversions // traversing over the array to find the number of times first element is smaller than second for(var i = 1; i < len; i++){ if (arr[i] < arr[i-1]){ count++; } } // checking if the last element of the array is smaller // than the first element or not and inversion must be equal to 1 if(arr[len-1] > arr[0] || count != 1){ return false; } else{ return true; } } // defining the array var arr = [5, 1, 2, 3, 4]; console.log("The given array is: ") console.log(arr); // calling to the function if(check(arr)){ console.log("Yes, the given array is sorted and rotated"); } else{ console.log("No, the given array is not sorted and rotated array") }

Time Complexity - O(N), where N is the size of the array.

Space Complexity - O(1), because we are not using any extra space.

## Conclusion

In this tutorial, we have discussed how to check an array is sorted and rotated. Here we have seen two approaches, the first is by finding a pivot element (which means minimum element and another is by counting adjacent inversion. The time and space complexity of both the approaches are same i.e., O(N) and O(1) respectively.