Rotated and sorted array
Check if array is rotated and sorted
class Solution {public: bool check(vector<int>& nums) { int cnt = 0; // To count the number of inversions (places where nums[i] < nums[i-1])
// Loop to check for inversions in the array for(int i = 1; i < nums.size(); i++) { if(nums[i] < nums[i-1]) { cnt++; // Increment count if an inversion is found } }
// Check if the last element is greater than the first element // This would indicate an inversion between the end and start of the array if(nums[nums.size()-1] > nums[0]) { cnt++; // Count this as an inversion as well }
// If there is 0 or 1 inversion, the array can be a rotated sorted array return cnt <= 1; }};Example:
vector<int> nums = {3, 4, 5, 1, 2};-
Initial Array:
nums[] = {3, 4, 5, 1, 2} -
Step-by-Step Execution:
- Loop over the array to count inversions:
- Compare
nums[1] (4)withnums[0] (3)→ No inversion. - Compare
nums[2] (5)withnums[1] (4)→ No inversion. - Compare
nums[3] (1)withnums[2] (5)→ Inversion!(1 < 5), socnt = 1. - Compare
nums[4] (2)withnums[3] (1)→ No inversion.
- Compare
- Boundary Check:
- Compare
nums[4] (2)withnums[0] (3)→ No inversion (2 < 3), so no increment incnt.
- Compare
- Loop over the array to count inversions:
-
Final Count of Inversions:
cnt = 1(1 inversion found).
-
Return Value:
- Since
cnt <= 1, the array is a valid rotated sorted array. - Output:
true.
- Since