#1752
Easy Algorithms Check if array is sorted and rotated
Array
55.7% acceptance
Feb 25, 2026
4870
285
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], for all valid values of i.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn check(nums: Vec<i32>) -> bool {
let n = nums.len();
let descents = (0..n).filter(|&i| nums[i] > nums[(i + 1) % n]).count();
descents <= 1
}
}