Binary Search - Check rotated sorted array
Sudeep .U

Sudeep .U @sudeep_c0d3s

About: Curios Engineer

Joined:
Feb 1, 2025

Binary Search - Check rotated sorted array

Publish Date: Jun 28
0 0

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

Comments 0 total

    Add comment