Rotation | Practice | GeeksforGeeks

Given an ascending sorted rotated array Arr of distinct integers of size N. The array is right rotated K times. Find the value of K.
Example 1:
Input:
N = 5
Arr[] = {5, 1, 2, 3, 4}
Output: 1
Explanation: The given array is 5 1 2 3 4. 
The original sorted array is 1 2 3 4 5. 
We can see that the array was rotated 
1 times to the right.

Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0
Explanation: The given array is not rotated.

Your Task:
Complete the function findKRotation() which takes array arr and size n, as input parameters and returns an integer representing the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <=105
1 <= Arri <= 107
 
0
ashutos17sharma89892 weeks ago
JAVA Binary Search 
 
class Solution {
    int findKRotation(int arr[], int n) {
        // code here
        int[] nums=arr;
            int low =0,high=nums.length-1;
        while(low<=high){
            int mid =(low+high)/2;
            int prev=mid==0?n-1:(mid-1);
            int next =mid==n-1?0:(mid+1);
            if((nums[mid]<=nums[next] && nums[mid]<=nums[prev]) ){
              return mid;
            }else if (nums[mid] > nums[high]) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return 0;
    }
}
-1
ashutos17sharma89892 weeks ago
class Solution {
    int findKRotation(int arr[], int n) {
        // code here
        for(int i=0;i<n-1;i++){
            if(arr[i]<arr[i+1]){
                continue;
            }
           return i+1; 
        }
        return 0;
    }
}
0
amrit_kumar2 weeks ago
binary search
 
int findKRotation(int arr[], int n) 
	{
	    int result = 0;
	    int l = 0;
	    int r = n-1;
	    while(l<=r)
	    {
	        int mid = l+(r-l)/2;
	        int left = (mid-1+n)%n;
	        int right = (mid+1)%n;
	        if(arr[left]>arr[mid]&&arr[mid]<arr[right])
	        {
	            return mid;
	        }
	        else if(arr[mid]<arr[r])
	        {
	            r = mid-1;
	        }
	        else
	        {
	            l= mid+1;
	        }
	    }
	    return result;
	}
0
parasrockz812 weeks ago
class Solution{public: int findKRotation(int arr[], int n) {    for(int x = 0;x < n-1 ;x++)    {        if(arr[x] > arr[x+1])        {            return x+1;        }    }    return 0;}
};
0
swapniltayal4223 weeks ago
public: int findKRotation(int arr[], int n) {    // code here        for (int i=1; i<n; i++){        if (arr[i] <= arr[i-1]){            return i;        }    }return 0;}
0
kumarsurajroy36553 weeks ago
// User function Template for Java
class Solution {   int findKRotation(int arr[], int n) {       // code here       int count=0;              for(int i=1;i<n;i++){                      if(arr[i-1]>arr[i] || count!=0) count++;       }              return (n-count)%n;   }}
0
ajay7yadav953 weeks ago
//javaScript lover ------------→   by A7
    findKRotation(arr,n) {       let count = 0;       for(let i=0;i<arr.length;i++){           if(arr[i]>arr[n-1])           {               count++;           }else{               break;           }       }       return count;   }
0
mohammadtanveer75403 weeks ago
class Solution{
public:	
	int findKRotation(int arr[], int n) {
        int ct=0, temp = arr[n-1];
        for(int i=0;i<n-1;i++){
            if(arr[i]>temp)
                ct++;
            else 
                break;
        }
        return ct;
	}
};
-3
uk85664 weeks ago
class Solution {   int findKRotation(int arr[], int n) {       int[] temp = new int[n];        System.arraycopy(arr,0,temp,0,n);       Arrays.sort(temp);       int firstVal= temp[0];       for(int i=0;i<n;i++){           if(firstVal == arr[i]){               return i;           }       }       return -1;   }}
0
araj626301 month ago
class Solution:
    def findKRotation(self,Arr,  n):
        start=0
        end=n-1
        while start<=end:
            if Arr[start] <= Arr[end]:
                return start
            mid=start+(end-start)//2
            nextt=(mid+1)%n
            prevv=(mid+n-1)%n
            if Arr[mid]<=Arr[nextt] and Arr[mid]<=Arr[prevv]:
                return mid
            elif Arr[mid] >= Arr[start]:
                start=mid+1
            else:
                end=mid-1

We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.