Beginner way to solve this question✅✅✅
IntuitionWe need to make the array non-decreasing by repeatedly merging the smallest consecutive pair until the array is sorted.Approach

Find the smallest cons
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to make the array non-decreasing by repeatedly merging the smallest consecutive pair until the array is sorted.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Find the smallest consecutive pair by their sum.

2. Merge this pair and repeat until the array is non-decreasing.

3. Track the number of operations.

# Complexity
- Time complexity:$$O(n^2)$$  due to repeated scans of the array.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(n)$$ for storing the modified array.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    bool isNondecreasing(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 ops = 0;
        while (!isNondecreasing(nums)) {
            int min_sum = INT_MAX;
            int index = -1;
            //step 1 -  Find the smallest consecutive pair by their sum.
            for (int i = 0; i < nums.size() - 1; i++) {
                int sum = nums[i] + nums[i + 1];
                if (sum < min_sum) {
                    min_sum = sum;
                    index = i;
                }
            }
            vector<int>temp;
            // step 2 - Merge this pair and repeat until the array is non-decreasing.
            for(int i = 0;i<nums.size();i++){
                if(i==index){
                    temp.push_back(nums[i]+nums[i+1]);
                    i++;
                }else{
                    temp.push_back(nums[i]);
                }
            }
            nums = temp;
           // step 3 - Track the number of operations.
            ++ops;
        }
        return ops;
    }
};
```
```python []
from typing import List

class Solution:
    def isNondecreasing(self, nums: List[int]) -> bool:
        for i in range(1, len(nums)):
            if nums[i] < nums[i - 1]:
                return False
        return True

    def minimumPairRemoval(self, nums: List[int]) -> int:
        ops = 0
        while not self.isNondecreasing(nums):
            min_sum = float('inf')
            index = -1
            # step 1 -  Find the smallest consecutive pair by their sum.
            for i in range(len(nums) - 1):
                sum_pair = nums[i] + nums[i + 1]
                if sum_pair < min_sum:
                    min_sum = sum_pair
                    index = i

            # step 2 - Merge this pair and repeat until the array is non-decreasing.
            temp = []
            i = 0
            while i < len(nums):
                if i == index:
                    temp.append(nums[i] + nums[i + 1])
                    i += 2  
                else:
                    temp.append(nums[i])
                    i += 1
            nums = temp
           # step 3 - Track the number of operations.
            ops += 1
        return ops
```
``` javascript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var minimumPairRemoval = function(nums) {
    // Helper function to check if the array is non-decreasing
    const isNondecreasing = (nums) => {
        for (let i = 1; i < nums.length; i++) {
            if (nums[i] < nums[i - 1]) {
                return false;
            }
        }
        return true;
    };

    let ops = 0;
    
    // Continue until the array is non-decreasing
    while (!isNondecreasing(nums)) {
        let minSum = Infinity;
        let index = -1;
        
        //step 1 -  Find the smallest consecutive pair by their sum.
        for (let i = 0; i < nums.length - 1; i++) {
            let sumPair = nums[i] + nums[i + 1];
            if (sumPair < minSum) {
                minSum = sumPair;
                index = i;
            }
        }
        
         // step 2 - Merge this pair and repeat until the array is non-decreasing.
        let temp = [];
        let i = 0;
        while (i < nums.length) {
            if (i === index) {
                temp.push(nums[i] + nums[i + 1]);
                i += 2;  // Skip next element as it is merged
            } else {
                temp.push(nums[i]);
                i++;
            }
        }
        
        nums = temp;
      // step 3 - Track the number of operations.
        ops++;
    }

    return ops;
};

```
['Simulation', 'C++', 'Python3', 'JavaScript']