CN: Number of rotations in sorted array
#binary-search
Last updated
#binary-search
Last updated
public class Solution {
public static int findKRotation(int []nums){
// Write your code here.
int low = 0, high = nums.length-1;
int rotations = 0;
int min = Integer.MAX_VALUE;
while(low <= high) {
int mid = low + (high-low)/2;
if(nums[low] <= nums[mid]) { // Left sorted
// Check if a new minimum is found and updated the min value
if(nums[low] < min) {
min = nums[low];
rotations = low; // Assign the current rotations to low in left sorted
}
low = mid + 1;
} else { // Right sorted
// Check if new minimum is found and update the min value
if(nums[mid] < min) {
min = nums[mid];
rotations = mid; // Assign the current rotations to mid in right sorted
}
high = mid -1;
}
}
return rotations;
}
}