Interview Questions/Coding/Rotated Sorted Array Check

Rotated Sorted Array Check

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Rotated Sorted Array Check

You are given an integer array nums. An array is considered sorted and rotated if it was originally sorted in non-decreasing order and then rotated some number of positions (possibly zero). Return "true" if nums can be obtained from a sorted non-decreasing array after some rotation. Otherwise, return "false". Input Format A single integer n representing the size of the array. n space-separated integers representing nums. Output Format Print "true" if the array is sorted and rotated, otherwise print "false".

Examples

Example 1
Input:
5
3 4 5 1 2

Output
true

Explanation
The array [1,2,3,4,5] was rotated three positions to form [3,4,5,1,2].
Output:
true

Explanation: The array [1,2,3,4,5] was rotated three positions to form [3,4,5,1,2].

Approach hint

Count how many times the sequence decreases while traversing the array.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp