Check if Array Is Sorted and Rotated LeetCode solution in Java
1class Solution {2public boolean check(int[] nums) {3// here we compare all the neighbouring elemnts and check whether they are in somewhat sorted4// 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 false6// else return true;7int irregularities = 0;8int length = nums.length;9for (int i = 0; i < length; i++) {10if (nums[i] > nums[(i + 1) % length]) irregularities += 1;11}12return irregularities > 1 ? false : true;13}14}