Problem
Given an array nums
, return true
if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false
.
There may be duplicates in the original array.
Note: An array A
rotated by x
positions results in an array B
of the same length such that A[i] == B[(i+x) % A.length]
, where %
is the modulo operation.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Solution
Video explanation
Here is the video explaining below methods in detail. Please check it out:
Method 1 - Naive
Now the naive approach can be:
- For each possible position in the array, we rotate the array to the left - in a way undoing the rotation to the right given in the original.
- Validate if the array is sorted in any of the rotations. If it is, then the array was rotated and sorted; otherwise, it was not.
Consider the array nums = [3, 4, 5, 1, 2]
.
- Rotation 0 (no rotation):
[3, 4, 5, 1, 2]
(not sorted) - Rotation 1:
[4, 5, 1, 2, 3]
(not sorted) - Rotation 2:
[5, 1, 2, 3, 4]
(not sorted) - Rotation 3:
[1, 2, 3, 4, 5]
(sorted, returntrue
)
Pseudocode
Here is the pseudocode:
|
|
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
. Generating rotations takesO(n^2)
because we generate a new array for each rotation.- Checking if an array is sorted takes
O(n)
. - So the overall complexity is
O(n^2)
.
- Checking if an array is sorted takes
- 🧺 Space complexity:
O(n)
for storing the rotated arrays.
Method 2 - Count the transitions
Here is the approach:
- Identify the number of places where the array transitions from a higher to a lower value.
- This transition can occur at most once in a valid rotated sorted array.
- If the transition happens more than once, then the array isn’t a valid rotated sorted array.
Lets take some examples.
- Consider
nums = [3, 4, 5, 1, 2]
. The array experiences a transition at5 -> 1
, which is one transition. Then,2 -> 3
is considered a continuation due to the circular nature, making it valid. - For
nums = [1, 3, 5, 1, 2]
, there is one transition at5 -> 1
. However,1
is not connected properly, indicating the array is not a valid sorted rotation. - For
nums = [1, 2, 3, 4, 5]
, there are no transitions from start to end. However, the last element5
compared to the first element1
forms one transition, which is valid. - For
nums = [1, 1, 1, 1]
, there are no transitions, indicating a valid extensively sorted array with no rotations required.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, where n is the length of the array, as we need to check each element. - 🧺 Space complexity:
O(1)
, as we are using a constant amount of extra space.
Method 3 - Doubling the array + Sliding Window
Consider nums = [3, 4, 5, 1, 2]
:
- The doubled array is
[3, 4, 5, 1, 2, 3, 4, 5, 1, 2]
.
|
|
- Use a sliding window of size
n = 5
:- Subarray
[3, 4, 5, 1, 2]
is not sorted. - Subarray
[4, 5, 1, 2, 3]
is not sorted. - Subarray
[5, 1, 2, 3, 4]
is not sorted. - Subarray
[1, 2, 3, 4, 5]
is sorted, so we returntrue
.
- Subarray
Here is the approach:
- Double the Array: Create a new array by concatenating the original array with itself. This helps in visualizing all possible rotations in a linear fashion.
- Find Sorted Window: Use a sliding window of size
n
to check if any of the subarrays of sizen
in the doubled array are sorted in non-decreasing order.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- Doubling the array takes
O(n)
time - Then, we check for subarrays of size n, and we may have to restart again in case we find an element which is not in sorted order, but we will start from the new point…and will go through each element only once in this doubled array, so it is
O(2n)
=O(n)
- Doubling the array takes
- 🧺 Space complexity:
O(n)
for storing the doubled array (O(2n)
=O(n)
)
Method 4 - Doubling the array using modulo operator + Sliding window
Here is the approach:
-
Initial Setup:
- Calculate the length of the array
n
. - Initialize
window_size
to track the current size of the sorted window.
- Calculate the length of the array
-
Sliding Window:
- Iterate through indices from
0
to2 * n - 1
to simulate a sliding window over the array. - For each
i
, use the modulo operator to access elements in a circular manner.
- Iterate through indices from
-
Check Sorted Window:
- Compare
nums[i % n]
withnums[(i + 1) % n]
. - If the elements are in non-decreasing order, increase
window_size
. - If they are not, reset
window_size
to 1. - If
window_size
reachesn
, returnTrue
.
- Compare
-
No Sorted Window Found:
- If no sorted window is found after checking all possible windows, return
False
.
- If no sorted window is found after checking all possible windows, return
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)
as now we are using modulo operator instead of doubling the array.