DEV Community

Sudeep .U
Sudeep .U

Posted on

Binary Search - Check rotated sorted array

Key points :
1) Find count of pivot elements
2) Edge case - check last element > first element
To check this - num[i] > num[(i+1)%nums.length]

class Solution {
    public boolean check(int[] nums) {
    int c = 0;
    for(int i = 0;i < nums.length; i++){
        if(nums[i] > nums[(i+1)%nums.length]){
            c++;
        }
        if(c > 1){
            return false;
        }
    }
    return true;

    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)