Answer: RUPEEK Hiring | On-Campus OA (2022) | Check if Array Is Sorted and Rotated
Answer · Posted Jun 2026
Approach A sorted and rotated array can have at most one position where the order decreases. Example: [3,4,5,1,2] ↑ There is only one "drop" (5 > 1). If more than one such drop exists, the array cannot be obtained from a sorted array through rotation. Strategy Traverse the array. Compare each element with the next element using circular indexing. Count the number of positions where:= nums[i] > nums[(i+1)%n] If the count exceeds 1, return false. Otherwise, return true. Java Code: ...
The full answer & interview discussion are available to premium members.
Log in Create a free account