C simple and easy solution with explanation
Here\'s a structured breakdown of your code for the function findLengthOfShortestSubarray, which finds the length of the shortest subarray that can be removed t
Here\'s a structured breakdown of your code for the function `findLengthOfShortestSubarray`, which finds the length of the shortest subarray that can be removed to make the remaining array sorted in non-decreasing order.\n\n---\n\n### Intuition\nTo solve this problem, we need to remove the smallest contiguous subarray from the given array `arr` so that the remaining elements form a non-decreasing sequence. Since removing the smallest subarray is desired, we should take advantage of any existing sorted segments.\n\n### Approach\n1. **Identify the Rightmost Sorted Segment**:\n   - Start by finding the rightmost part of the array that is already sorted in non-decreasing order, from the end towards the start.\n   - Use a pointer `r` to keep track of this index, and decrement it while the elements satisfy the non-decreasing condition.\n   - The variable `r` now represents the starting index of the rightmost sorted segment.\n   \n2. **Check if Array is Already Sorted**:\n   - If `r` is `0`, the entire array is already sorted. Hence, we can return `0` as no subarray needs to be removed.\n\n3. **Iterate Over Possible Left Segments**:\n   - Use another pointer `i` to iterate over the left part of the array, checking for each possible endpoint of a sorted segment starting from the beginning.\n   - For each `i`, advance `r` until `arr[i] <= arr[r]`, ensuring that combining the left part up to `i` and the right part from `r` forms a sorted sequence.\n   - Update `ans` with the minimum length of the subarray to be removed, calculated as `r - i - 1`.\n\n4. **Return the Minimum Subarray Length**:\n   - The result stored in `ans` gives the minimum length of the subarray that, when removed, leaves the array sorted.\n\n### Complexity\n\n- **Time Complexity**:  \n  The time complexity is \\( O(n) \\) because both pointers `i` and `r` move at most `n` steps, and each step requires constant-time comparisons.\n\n- **Space Complexity**:  \n  The space complexity is \\( O(1) \\) since only a fixed amount of additional space is used for variables.\n\n---\n\n\n# Code\n```c []\nint findLengthOfShortestSubarray(int* arr, int n) \n{\n    int r = n - 1;\n    while (r > 0 && arr[r - 1] <= arr[r]) \n    {\n        --r;\n    }\n    int ans = r;\n    for (int i = 0; i < r && (i == 0 || arr[i - 1] <= arr[i]); ++i) \n    {\n        while (r < n && arr[r] < arr[i]) \n        {\n            ++r;\n        }\n        ans = (r - i - 1) < ans ? (r - i - 1) : ans;\n    }\n\n    return ans;\n}\n```
['Array', 'C']