Check if Array Is Sorted and Rotated LeetCode solution in Java

1
class Solution {
2
public boolean check(int[] nums) {
3
// here we compare all the neighbouring elemnts and check whether they are in somewhat sorted
4
// there will be a small change due to rotation in the array at only one place.
5
// so if there are irregularities more than once, return false
6
// else return true;
7
int irregularities = 0;
8
int length = nums.length;
9
for (int i = 0; i < length; i++) {
10
if (nums[i] > nums[(i + 1) % length]) irregularities += 1;
11
}
12
return irregularities > 1 ? false : true;
13
}
14
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0