Easy to understand In-place Solution | Beats 100% time and space | C++
IntuitionThe core idea is to simulate the process directly as described, modifying the input array in-place. We repeatedly find the adjacent pair with the small
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The core idea is to simulate the process directly as described, modifying the input array in-place. We repeatedly find the adjacent pair with the smallest sum (prioritizing the leftmost), merge them by updating the left element's value and effectively removing the right element by shifting subsequent elements within the original array. This continues until the array becomes non-decreasing, and we count the operations.

# Approach
1.  **Loop Condition:** Continuously perform operations while the array, considering its current effective size, is not non-decreasing. Keep track of the operation count (`res`).
2.  **Find Min Pair:** In each iteration, scan adjacent pairs `(nums[i-1], nums[i])` within the current effective size. Identify the pair with the minimum sum, prioritizing the leftmost pair (smallest `i`) in case of ties. Let `min_idx` be the index of the second element (`nums[i]`) in this pair.
3.  **In-Place Merge & Shrink:**
    * Update the left element of the pair with the sum: `nums[min_idx-1] += nums[min_idx]`.
    * Effectively remove the right element (`nums[min_idx]`) by shifting all subsequent elements one position to the left.
    * Decrement the effective size of the array and increment the operation count `res`.
4.  **Return Count:** When the loop terminates (array is non-decreasing), return the total count `res`.

## Example Walkthrough (`nums = [5, 2, 3, 1]`)

1.  **Initial:** `nums = [5, 2, 3, 1]`, `current_size = 4`, `res = 0`. Array is not non-decreasing.
2.  **Iteration 1:**
    * Pairs & Sums: `(5,2)` sum=7; `(2,3)` sum=5; `(3,1)` sum=4.
    * Minimum sum is 4 for pair `(3,1)`. `min_idx = 3` (index of `1`).
    * Merge: `nums[min_idx-1] = nums[2] += nums[3]`. `nums[2]` becomes `3 + 1 = 4`. Array state: `[5, 2, 4, 1]`.
    * Shift: Element at index `min_idx` (which is 3) needs to be overwritten. Since it's the last element, no shift is practically needed, but the effective size decreases.
    * `current_size` becomes 3. `res` becomes 1.
    * Check `is_increasing` for effective array `[5, 2, 4]`: Returns `false` (because 2 < 5).
3.  **Iteration 2:**
    * Pairs & Sums (within effective size 3): `(5,2)` sum=7; `(2,4)` sum=6.
    * Minimum sum is 6 for pair `(2,4)`. `min_idx = 2` (index of `4`).
    * Merge: `nums[min_idx-1] = nums[1] += nums[2]`. `nums[1]` becomes `2 + 4 = 6`. Array state: `[5, 6, 4, 1]`.
    * Shift: Element at index `min_idx` (which is 2) needs to be overwritten. `nums[2] = nums[3]` (conceptually, as `current_size` will shrink).
    * `current_size` becomes 2. `res` becomes 2.
    * Check `is_increasing` for effective array `[5, 6]`: Returns `true`.
4.  **Terminate:** The `while` loop condition `!is_increasing(nums, current_size)` is now false.
5.  **Return:** `res = 2`.

# Complexity
- Time complexity: $$O(N^2)$$ 
    -  Where n -> size of nums.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    bool is_increasing(vector <int> &arr, int curr_n){
        for(int i=1; i<curr_n; i++){
            if(arr[i]<arr[i-1])return false;
        }
        return true;
    }
    int minimumPairRemoval(vector<int>& nums) {
        int n = nums.size();
        if(n==1) return 0;

        int res = 0;
        int current_size = n;

        // Loop until the array becomes non-decreasing
        while(!is_increasing(nums, current_size)){
            int min_idx = 1;
            int min = nums[0]+nums[1];
            int sum;

            // Find the leftmost adjacent pair with the minimum sum
            for(int i=1; i<current_size; i++){
                sum = nums[i]+nums[i-1];
                if(sum<min){
                    min_idx = i;
                    min = sum;
                }
            }

            // Perform the merge: add the value of the second element to the first
            nums[min_idx-1]+=nums[min_idx];

            // Shift elements to remove the second element of the pair
            for(int j=min_idx; j<(current_size-1); j++){
                nums[j] = nums[j+1];
            } 
            res++; // Increment the operation count
            current_size--; // Decrease the effective size of the array
        }
        return res;
    }
};
```
['C++']