🚀 Best C++ Solution | 0ms Runtime | Time : O(n²) | Easy to Understand.
💡IntuitionIf the array is already sorted, we don’t need to do anything.Otherwise, we keep removing the pair of neighbors with the smallest total, and replace it
# 💡Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
If the array is already sorted, we don’t need to do anything.

Otherwise, we keep removing the pair of neighbors with the smallest total, and replace it with their sum. This helps fix the "unsorted" parts of the array, step-by-step.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Check if the array is sorted.

2. While it’s not sorted:

    - Look at all side-by-side pairs.

    - Find the pair with the smallest sum, and if there are many, pick the first one.

    - Replace that pair with their sum (remove one element).

    - Count this as one operation.

3. Repeat until the array is sorted.

4. Return the total number of operations.
# Complexity
- Time complexity: `O(n^2)`
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```cpp []
class Solution {
public:
       bool isNonDecreasing(const vector<int>& nums) {
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] < nums[i - 1]) return false;
        }
        return true;
    }

    int minimumPairRemoval(vector<int>& nums) {
        int operations = 0;
        
        while (!isNonDecreasing(nums)) {
            int minSum = INT_MAX;
            int minIndex = -1;

            // Find the leftmost adjacent pair with minimum sum
            for (int i = 0; i < nums.size() - 1; ++i) {
                int pairSum = nums[i] + nums[i + 1];
                if (pairSum < minSum) {
                    minSum = pairSum;
                    minIndex = i;
                }
            }

            // Replace the pair with their sum
            nums[minIndex] = nums[minIndex] + nums[minIndex + 1];
            nums.erase(nums.begin() + minIndex + 1);
            ++operations;
        }

        return operations;
    }
};

```


# 🔍 How the Code Works with Example

`nums = [5, 2, 3, 1]`
## 🔄 Steps :
1. Not sorted → Find pair with smallest sum:

    - (3,1) → sum = 4 → Replace with 4 → `nums = [5, 2, 4]`
    - Operations = 1

2. Still not sorted → Find next smallest pair:

    - (2,4) → sum = 6 → Replace with 6 → `nums = [5, 6]`
    - Operations = 2

3. Now `[5, 6]` is sorted → Done ✅

- Return Number of Operations, Which is 2.

Final Output : `2`
['Array', 'Simulation', 'C++']