Open In App

Check if an array is sorted and rotated using Binary Search

Pre-requisite: Check if an array is sorted and rotated using Linear Search
Given an array arr[] of N distinct integers, the task is to check if this array is sorted when rotated counter-clockwise. A sorted array is not considered sorted and rotated, i.e., there should at least one rotation.

Examples: 



Input: arr[] = { 3, 4, 5, 1, 2 } 
Output: true 
Explanation: 
Sorted array: {1, 2, 3, 4, 5}. 
Rotating this sorted array clockwise 
by 3 positions, we get: { 3, 4, 5, 1, 2}

Input: arr[] = {7, 9, 11, 12, 5} 
Output: true



Input: arr[] = {1, 2, 3} 
Output: false 

Approach: One approach to solving this problem using Linear Search has already been discussed in this article
In this article, an approach using Binary Search concept is mentioned. 

if (high < low)
     return -1;
if (high == low)
     return low;
if (mid < high && arr[mid + 1] < arr[mid])
    return mid;
if (mid > low && arr[mid] < arr[mid - 1])
    return mid - 1;
if (arr[low] > arr[mid]) 
    return findPivot(arr, low, mid - 1);    
else 
    return findPivot(arr, mid + 1, high);

Below is the implementation of the above approach: 




#include <bits/stdc++.h>
 
using namespace std;
 
// Function to return the
// index of the pivot
int findPivot(int arr[], int low, int high)
{
    // Base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;
 
    int mid = (low + high) / 2;
    if (mid < high && arr[mid + 1] < arr[mid])
    {
        return mid;
    }
 
    // Check if element at (mid - 1) is pivot
    // Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low && arr[mid] < arr[mid - 1])
    {
        return mid - 1;
    }
 
    // Decide whether we need to go to
    // the left half or the right half
    if (arr[low] > arr[mid])
    {
        return findPivot(arr, low, mid - 1);
    }
    else
    {
        return findPivot(arr, mid + 1, high);
    }
}
 
// Function to check if a given array
// is sorted rotated or not
bool isRotated(int arr[], int n)
{
    int l = 0;
    int r = n - 1;
    int pivot = -1;
    if (arr[l] > arr[r])
    {
        pivot = findPivot(arr, l, r);
        int temp=pivot;
        // To check if the elements to the left
        // of the pivot are in descending or not
        if (l < pivot)
        {
            while (pivot > l)
            {
                if (arr[pivot] < arr[pivot - 1])
                {
                    return false;
                }
                pivot--;
            }
        }
 
        // To check if the elements to the right
        // of the pivot are in ascending or not
        pivot=temp;
        if(pivot < r) {
            pivot++;
            while (pivot < r) {
                if (arr[pivot] > arr[pivot + 1]) {
                    return false;
                }
                pivot++;
            }
        }
 
        // If both of the above if is true
        // Then the array is sorted rotated
        return true;
    }
 
    // Else the array is not sorted rotated
    else {
        return false;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 5, 1, 2};
    if (isRotated(arr, 5)) cout<<"true";
    else
    cout<<"false";
    return 0;
}
 
// This code is contributed by mohit kumar 29




// Java implementation of the above approach
 
class GFG {
    // Function to return the
    // index of the pivot
    static int findPivot(int arr[], int low, int high)
    {
        // Base cases
        if (high < low)
            return -1;
        if (high == low)
            return low;
 
        int mid = (low + high) / 2;
        if (mid < high && arr[mid + 1] < arr[mid]) {
            return mid;
        }
 
        // Check if element at (mid - 1) is pivot
        // Consider the cases like {4, 5, 1, 2, 3}
        if (mid > low && arr[mid] < arr[mid - 1]) {return mid - 1;}
        // Decide whether we need to go to
        // the left half or the right half
        if (arr[low] > arr[mid]) {return findPivot(arr, low, mid - 1);}
        else {
            return findPivot(arr, mid + 1, high);
        }
    }
 
    // Function to check if a given array
    // is sorted rotated or not
    public static boolean isRotated(int arr[], int n)
    {
        int l = 0;
        int r = n - 1;
        int pivot = -1;
        if (arr[l] > arr[r]) {
            pivot = findPivot(arr, l, r);
            int temp=pivot;
            // To check if the elements to the left
            // of the pivot are in descending or not
            if (l < pivot) {
                while (pivot > l) {
                    if (arr[pivot] < arr[pivot - 1]) {
                        return false;
                    }
                    pivot--;
                }
            }
 
            // To check if the elements to the right
            // of the pivot are in ascending or not
         
            pivot=temp;
            if(pivot < r) {
                pivot++;
                while (pivot < r) {
                    if (arr[pivot] > arr[pivot + 1]) {
                        return false;
                    }
                    pivot++;
                }
            }
 
            // If any of the above if or else is true
            // Then the array is sorted rotated
            return true;
        }
 
        // Else the array is not sorted rotated
        else {
            return false;
        }
    }
    // Driver code
    public static void main(String[] args){
        int arr[] = { 3, 4, 5, 1, 2  };
        System.out.println(isRotated(arr, 5));
    }
}
//this code it contributed by Ajay Singh




# Python3 implementation of the above approach
 
# Function to return the
# index of the pivot
def findPivot(arr, low, high) :
 
    # Base cases
    if (high < low) :
        return -1;
         
    if (high == low) :
        return low;
 
    mid = (low + high) // 2;
    if (mid < high and arr[mid + 1] < arr[mid]) :
     
        return mid;
 
    # Check if element at (mid - 1) is pivot
    # Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low and arr[mid] < arr[mid - 1]) :
     
        return mid - 1;
     
    # Decide whether we need to go to
    # the left half or the right half
    if (arr[low] > arr[mid]) :
     
        return findPivot(arr, low, mid - 1);
     
    else :
     
        return findPivot(arr, mid + 1, high);
     
# Function to check if a given array
# is sorted rotated or not
def isRotated(arr, n) :
 
    l = 0;
    r = n - 1;
    pivot = -1;
    if (arr[l] > arr[r]) :
     
        pivot = findPivot(arr, l, r);
        temp = pivot
        # To check if the elements to the left
        # of the pivot are in descending or not
         
        if (l < pivot) :
         
            while (pivot > l) :
             
                if (arr[pivot] < arr[pivot - 1]) :
                 
                    return False;
                 
                pivot -= 1;
 
        # To check if the elements to the right
        # of the pivot are in ascending or not
         
        else :
            pivot=temp
            pivot += 1;
            while (pivot < r) :
                if (arr[pivot] > arr[pivot + 1]) :
                    return False;
                 
                pivot += 1;
     
        # If any of the above if or else is true
        # Then the array is sorted rotated
        return True;
 
    # Else the array is not sorted rotated
    else :
        return False;
 
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 3, 4, 5, 1, 2 ];
    if (isRotated(arr, 5)) :
        print("True");
    else :
        print("False");
 
# This code is contributed by Yash_R




// C# implementation of the above approach
using System;
 
class GFG {
  
    // Function to return the
    // index of the pivot
    static int findPivot(int []arr, int low, int high)
    {
        // Base cases
        if (high < low)
            return -1;
        if (high == low)
            return low;
  
        int mid = (low + high) / 2;
        if (mid < high && arr[mid + 1] < arr[mid]) {
            return mid;
        }
  
        // Check if element at (mid - 1) is pivot
        // Consider the cases like {4, 5, 1, 2, 3}
        if (mid > low && arr[mid] < arr[mid - 1]) {
            return mid - 1;
        }
  
        // Decide whether we need to go to
        // the left half or the right half
        if (arr[low] > arr[mid]) {
            return findPivot(arr, low, mid - 1);
        }
        else {
            return findPivot(arr, mid + 1, high);
        }
    }
  
    // Function to check if a given array
    // is sorted rotated or not
    public static bool isRotated(int []arr, int n)
    {
        int l = 0;
        int r = n - 1;
        int pivot = -1;
        if (arr[l] > arr[r]) {
            pivot = findPivot(arr, l, r);
            int temp = pivot;
            // To check if the elements to the left
            // of the pivot are in descending or not
            if (l < pivot) {
                while (pivot > l) {
                    if (arr[pivot] < arr[pivot - 1]) {
                        return false;
                    }
                    pivot--;
                }
            }
  
            // To check if the elements to the right
            // of the pivot are in ascending or not
            pivot=temp;
            else {
                pivot++;
                while (pivot < r) {
                    if (arr[pivot] > arr[pivot + 1]) {
                        return false;
                    }
                    pivot++;
                }
            }
  
            // If any of the above if or else is true
            // Then the array is sorted rotated
            return true;
        }
  
        // Else the array is not sorted rotated
        else {
            return false;
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 3, 4, 5, 1, 2 };
        Console.WriteLine(isRotated(arr, 5));
    }
}
 
// This code contributed by Rajput-Ji




<script>
 
// Function to return the
// index of the pivot
function findPivot(arr, low, high)
{
    // Base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;
 
    var mid = parseInt((low + high) / 2);
    if (mid < high && arr[mid + 1] < arr[mid])
    {
        return mid;
    }
 
    // Check if element at (mid - 1) is pivot
    // Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low && arr[mid] < arr[mid - 1])
    {
        return mid - 1;
    }
 
    // Decide whether we need to go to
    // the left half or the right half
    if (arr[low] > arr[mid])
    {
        return findPivot(arr, low, mid - 1);
    }
    else
    {
        return findPivot(arr, mid + 1, high);
    }
}
 
// Function to check if a given array
// is sorted rotated or not
function isRotated(arr, n)
{
    var l = 0;
    var r = n - 1;
    var pivot = -1;
    if (arr[l] > arr[r])
    {
        pivot = findPivot(arr, l, r);
        var temp=pivot;
        // To check if the elements to the left
        // of the pivot are in descending or not
        if (l < pivot)
        {
            while (pivot > l)
            {
                if (arr[pivot] < arr[pivot - 1])
                {
                    return false;
                }
                pivot--;
            }
        }
 
        // To check if the elements to the right
        // of the pivot are in ascending or not
         
        else
        {
            pivot=temp;
            pivot++;
            while (pivot < r) {
                if (arr[pivot] > arr[pivot + 1]) {
                    return false;
                }
                pivot++;
            }
        }
 
        // If both of the above if is true
        // Then the array is sorted rotated
        return true;
    }
 
    // Else the array is not sorted rotated
    else {
        return false;
    }
}
 
// Driver code
var arr = [4, 5, 1, 3, 2];
if (isRotated(arr, 5))
    document.write("true");
else
    document.write("false");
 
</script>

Output: 
true

 

Time Complexity: O(N) as: 

 


Article Tags :