Check if an array is sorted and rotated
Last Updated :
30 Jul, 2024
Given an array arr[] of size n, the task is to return true if it was originally sorted in non-decreasing order and then rotated (including zero rotations). Otherwise, return false. The array may contain duplicates.
Examples:
Input: arr[] = { 3, 4, 5, 1, 2 }
Output: YES
Explanation: The above array is sorted and rotated
Sorted array: {1, 2, 3, 4, 5}
Rotating this sorted array clockwise
by 3 positions, we get: { 3, 4, 5, 1, 2}
Input: arr[] = {3, 4, 6, 1, 2, 5}
Output: NO
Explanation: The above array can't be rotated clockwise to form the sorted array.
[Expected Approach]: By checking the inversions of adjacent elements
As we know, a sorted array of n elements (which can contain duplicates) follows the property where the previous element is smaller or equal to the current element. So, if we rotate it by some point (Clockwise or Anticlockwise) , there exists a pivot where the previous element is greater or equal to the current element. If there is at most one occurrence of pivot then we can say array is rotated and sorted.
Follow the given steps to solve the problem:
- Define a variable, say count and initialize it as 0.
- Traverse from the first element to the last element and if the next element is smaller than the current element, increment count of adjacent inversions by 1.
- For the last element, if it is greater than the first element, increment count of adjacent inversions by 1.
- After traversing the array:
- If the count of inversions is less than or equal to 1, it means that the array is sorted and rotated, return true.
- Else the array is not sorted and rotated, return false.
Below is the implementation of the above approach:
C++
//C++ program to check an array is sorted and rotated
#include <bits/stdc++.h>
using namespace std;
// Function to check if arr[] is rotated and sorted
bool check(vector<int>& arr, int n)
{
// Initialize count of the number of times the sequence
// is out of order
int count = 0;
// Iterate through the vector
for (int i = 0; i < n; i++) {
// Check if the current element is greater than the
// next element
if (arr[i] > arr[(i + 1) % n]) {
// Increment count if the sequence is out of
// order
count++;
}
}
// Return true if there is at most one point where the
// sequence is out of order
return count <= 1;
}
// Driver Code
int main()
{
// Sample Input
// Example of a rotated and sorted array
vector<int> arr = { 3, 4, 5, 1, 2 };
// Get the size of the vector
int n = arr.size();
// Call the check function and determine if the array is
// rotated and sorted
if (check(arr, n))
// Print YES if the array is rotated and sorted
cout << "YES" << endl;
else
// Print NO if the array is not rotated and sorted
cout << "NO" << endl;
return 0;
}
Java
// java program to check an array is sorted and rotated
import java.util.Arrays;
public class GFG {
// Function to check if arr[] is rotated and sorted
public static boolean check(int[] arr, int n)
{
// Initialize count of the number of times the
// sequence is out of order
int count = 0;
// Iterate through the array
for (int i = 0; i < n; i++) {
// Check if the current element is greater than
// the next element
if (arr[i] > arr[(i + 1) % n]) {
// Increment count if the sequence is out of
// order
count++;
}
}
// Return true if there is at most one point where
// the sequence is out of order
return count <= 1;
}
// Driver Code
public static void main(String[] args)
{
// Sample Input
// Example of a rotated and sorted array
int[] arr = { 3, 4, 5, 1, 2 };
// Get the size of the array
int n = arr.length;
// Call the check function and determine if the
// array is rotated and sorted
if (check(arr, n))
// Print YES if the array is rotated and sorted
System.out.println("YES");
else
// Print NO if the array is not rotated and
// sorted
System.out.println("NO");
}
}
Python
#Python program to check an array is sorted and rotated
def check(arr):
# Initialize count of the number of times the sequence
# is out of order
count = 0
n = len(arr) # Get the size of the list
# Iterate through the list
for i in range(n):
# Check if the current element is greater than the
# next element
if arr[i] > arr[(i + 1) % n]:
# Increment count if the sequence is out of
# order
count += 1
# Return true if there is at most one point where the
# sequence is out of order
return count <= 1
# Driver Code
if __name__ == "__main__":
# Sample Input
# Example of a rotated and sorted array
arr = [3, 4, 5, 1, 2]
# Call the check function and determine if the array is
# rotated and sorted
if check(arr):
# Print YES if the array is rotated and sorted
print("YES")
else:
# Print NO if the array is not rotated and sorted
print("NO")
C#
// C# program to check an array is sorted and rotated
using System;
using System.Collections.Generic;
class Program {
// Function to check if arr[] is rotated and sorted
static bool Check(int[] arr)
{
// Initialize count of the number of times the
// sequence is out of order
int count = 0;
int n = arr.Length; // Get the size of the array
// Iterate through the array
for (int i = 0; i < n; i++) {
// Check if the current element is greater than
// the next element
if (arr[i] > arr[(i + 1) % n]) {
// Increment count if the sequence is out of
// order
count++;
}
}
// Return true if there is at most one point where
// the sequence is out of order
return count <= 1;
}
// Driver Code
static void Main()
{
// Sample Input
// Example of a rotated and sorted array
int[] arr = { 3, 4, 5, 1, 2 };
// Call the check function and determine if the
// array is rotated and sorted
if (Check(arr))
// Print YES if the array is rotated and sorted
Console.WriteLine("YES");
else
// Print NO if the array is not rotated and
// sorted
Console.WriteLine("NO");
}
}
JavaScript
// Javascript program to check an array is sorted and
// rotated
function check(arr)
{
// Initialize count of the number of times the sequence
// is out of order
let count = 0;
let n = arr.length; // Get the size of the array
// Iterate through the array
for (let i = 0; i < n; i++) {
// Check if the current element is greater than the
// next element
if (arr[i] > arr[(i + 1) % n]) {
// Increment count if the sequence is out of
// order
count++;
}
}
// Return true if there is at most one point where the
// sequence is out of order
return count <= 1;
}
// Driver Code
// Example of a rotated and sorted array
const arr = [ 3, 4, 5, 1, 2 ];
// Call the check function and determine if the array is
// rotated and sorted
if (check(arr)) {
// Print YES if the array is rotated and sorted
console.log("YES");
}
else {
// Print NO if the array is not rotated and sorted
console.log("NO");
}
Time Complexity: O(N), where N is the size of array.
Auxiliary Space: O(1)
Similar Reads
Minimum in a Sorted and Rotated Array Given a sorted array of distinct elements arr[] of size n that is rotated at some unknown point, the task is to find the minimum element in it. Examples: Input: arr[] = [5, 6, 1, 2, 3, 4]Output: 1Explanation: 1 is the minimum element present in the array.Input: arr[] = [3, 1, 2]Output: 1Explanation:
9 min read
Javascript Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar
3 min read
Check if an array is Wave Array Given an array of N positive integers. The task is to check if the array is sorted in wave form. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: NO Input: arr[] = {1, 5, 3, 7, 2, 8, 6}Output: YES Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach: First c
14 min read
Sort a Rotated Sorted Array You are given a rotated sorted array and your aim is to restore its original sort in place.Expected to use O(1) extra space and O(n) time complexity. Examples: Input : [3, 4, 1, 2] Output : [1, 2, 3, 4] Input : [2, 3, 4, 1] Output : [1, 2, 3, 4] We find the point of rotation. Then we rotate array us
11 min read
Rotation Count in a Rotated Sorted array Given an array arr[] having distinct numbers sorted in increasing order and the array has been right rotated (i.e, the last element will be cyclically shifted to the starting position of the array) k number of times, the task is to find the value of k.Examples: Input: arr[] = {15, 18, 2, 3, 6, 12}Ou
12 min read