Check if Array is Soted and Rotated Leer

 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.


 


Example 1:


Input: nums = [3,4,5,1,2]

Output: true

Explanation: [1,2,3,4,5] is the original sorted array.

You can rotate the array by x = 3 positions to begin on the element of value 3: [3,4,5,1,2].

Example 2:


Input: nums = [2,1,3,4]

Output: false

Explanation: There is no sorted array once rotated that can make nums.

Example 3:


Input: nums = [1,2,3]

Output: true

Explanation: [1,2,3] is the original sorted array.

You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.

Constraints:


1 <= nums.length <= 100

1 <= nums[i] <= 100



Solution :-



class Solution {
    public boolean check(int[] nums) {
       
         int count = 0; // To count breakpoints
        int n = nums.length;
       
        for (int i = 0; i < n; i++) {
            // Circular check using (i + 1) % n
            if (nums[i] > nums[(i + 1) % n]) {
                count++;
            }
           
            // If there is more than one breakpoint, return false
            if (count > 1) {
                return false;
            }
        }
       
        return true; // If count is 0 or 1, it's a valid sorted & rotated array
    }
}



Final Conclusion

  • Count breakpoints (nums[i] > nums[i+1]).
  • If count ≤ 1 → Return true (sorted & rotated).
  • If count > 1 → Return false (not sorted & rotated).

Key Observations

  1. A sorted array (like [1, 2, 3, 4, 5]) should return true.

    • If there are zero breakpoints (nums[i] > nums[i+1] never happens), it should still be considered "sorted and rotated" because rotating n times brings it back to the original.
  2. A sorted and rotated array (like [3, 4, 5, 1, 2]) should also return true.

    • Here, we have exactly one breakpoint (5 > 1).
  3. An unsorted array (like [2, 1, 3, 4]) should return false.

    • It has more than one breakpoint (2 > 1 and 4 > 2).
Why Does This Work?
Case 1: Already Sorted (No Rotation)

nums = [1, 2, 3, 4, 5]
No breakpoints → count = 0
✅ Valid Sorted & Rotated (because it can be rotated n times)
Case 2: Sorted & Rotated


nums = [3, 4, 5, 1, 2]
One breakpoint (5 > 1)
✅ Valid Sorted & Rotated
Case 3: Not Sorted & Rotated


nums = [2, 1, 3, 4]
Two breakpoints (2 > 1, 4 > 2)
❌ Not a valid sorted & rotated array

Final Conclusion
Count breakpoints (nums[i] > nums[i+1]).
If count ≤ 1 → Return true (sorted & rotated).
If count > 1 → Return false (not sorted & rotated).
This approach correctly handles all cases, including fully sorted arrays that were rotated n times. 🚀😊

Let me know if anything needs further clarification

You said:
         what do you means by breakpoint explain please ?                                                                                                                             
What is a Breakpoint?
A breakpoint is a position in the array where the order violates the increasing sequence.

Example 1: Sorted & Rotated Array

nums = [3, 4, 5, 1, 2]
Look at each adjacent pair:

3 < 4  ✅ (Sorted)
4 < 5  ✅ (Sorted)
5 > 1  ❌ (Not sorted) → **Breakpoint**
1 < 2  ✅ (Sorted)
There is only one breakpoint at 5 > 1, so this is a valid sorted & rotated array.
Example 2: Already Sorted Array

nums = [1, 2, 3, 4, 5]
Checking adjacent pairs:

1 < 2 ✅
2 < 3 ✅
3 < 4 ✅
4 < 5 ✅
5 < 1 ✅ (Circular check)
No breakpoints → Still considered a valid sorted & rotated array (since rotating n times keeps it the same).


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence