Given nums, return true if it was originally sorted in non-decreasing order and then rotated some number of positions (zero included). Duplicates are allowed.
A sorted array has no place where a value is bigger than the next one. Rotating it creates exactly one such drop, at the rotation point. If you also compare the last element with the first, a valid array has at most one drop in total.
def check(nums):
n = len(nums)
drops = 0
for i in range(n):
if nums[i] > nums[(i + 1) % n]:
drops += 1
return drops <= 1
An array of identical values has zero drops and is valid. A single element is valid. Equal neighbours are not drops, which is why the comparison is strictly greater than.
It is O(n) time, O(1) space, and it replaces trying every rotation with one invariant you can state in a sentence. Interviewers remember candidates who name the invariant first.
Practising this kind of explanation is what Linkjob AI is for (disclosure: I work there).
Disclosure: I work on Linkjob AI.