check if array is sorted or rotated array is sorted

 class Solution {

    public boolean check(int[] nums) {
  int countDrops = 0;
int n=nums.length;
    for (int i = 0; i < n; i++) {
        int next = (i + 1) % n; // wrap around for rotation
        if (nums[i] > nums[next]) {
            countDrops++;
        }
    }

    return countDrops <= 1; // at most 1 drop allowed      
}
}

Comments

Popular Posts