Find the rotation count in clockwise rotated sorted array(method -1)

Question -
To calculate the no of clockwise rotation in sorted and rotated array

Example - 
Input : arr[]  = {4,5,1,2,3} , n=5
Output : 2

Input : arr[] = {10,12,14,2,5} n=5
Output : 3

Approach - 

Using a for loop we check that if there element is greater than next element then element index+1 is the no of rotation.

Implementation - 

//using linear search
#include<iostream>
using namespace std;
int countrotation(int arr[],int n)
{
    for(int i=0;i<n;i++){
        if(arr[i]>arr[i+1])
        return i+1;
    }
}
int main()
{
    int n;
    cout<<"Enter the no of element : ";cin>>n;
    int arr[n];
    cout<<"Enter elements : "<<endl;
    for(int i=0;i<n;i++)
    cin>>arr[i];
    int count=countrotation(arr,n);
    cout<<"No of rotation is : "<<count<<endl;
    
    return 0;
    
}

Time complixity - O(n)

Comments