# Minimum pair merge operations required to make Array non-increasing A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Given an array A[], the task is to find the minimum number of operations required in which two adjacent elements are removed from the array and replaced by their sum, such that the array is converted to a non-increasing array.

Note: An array with a single element is considered as non-increasing.

Examples:

Input:_ A[] = {1, 5, 3, 9, 1}_

Output:_ 2_

Explanation:

Replacing {1, 5} by {6} modifies the array to {6, 3, 9, 1}

Replacing {6, 3} by {9} modifies the array to {9, 9, 1}

Input:_ A[] = {0, 1, 2}_

Output:_ 2_

*Approach: *The idea is to use Dynamic Programming. A memoization table is used to store the minimum count of operations required to make subarrays non-increasing from right to left of the given array. Follow the steps below to solve the problem:

• Initialize an array dp[] where dp[i] stores the minimum number of operations required to make the subarray {A[i], …, A[N]} non-increasing. Therefore, the target is to compute dp.
• Find a minimal subarray {A[i] .. A[j]} such that sum({A[i] .. A[j]}) > val[j+1], where, val[j + 1] is the merged sum obtained for the subarray {A[j + 1], … A[N]}.
• Update dp[i] to j – i + dp[j+1] *and *vals[i] to sum({A[i] .. A[j]}).

Below is the implementation of the above approach:

• C++

filter_none

edit

play_arrow

brightness_4

`// C++ program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`// Function to find the minimum operations`

`// to make the array Non-increasing`

`**int**` `solve(vector<``**int**``>& a)`

`{`

`// Size of the array`

`**int**` `n = a.size();`

`// Dp table initialization`

`vector<``**int**``> dp(n + 1, 0), val(n + 1, 0);`

`// dp[i]: Stores minimum number of `

`// operations required to make `

`// subarray {A[i], ..., A[N]} non-increasing`

`**for**` `(``**int**` `i = n - 1; i >= 0; i--) {`

`**long**` `**long**` `sum = a[i];`

`**int**` `j = i;`

`**while**` `(j + 1 < n and sum < val[j + 1]) {`

`// Increment the value of j`

`j++;`

`// Add current value to sum`

`sum += a[j];`

`}`

`// Update the dp tables`

`dp[i] = (j - i) + dp[j + 1];`

`val[i] = sum;`

`}`

`// Return the answer`

`**return**` `dp;`

`}`

`// Driver code`

`**int**` `main()`

`{`

`vector<``**int**``> arr = { 1, 5, 3, 9, 1 };`

`cout << solve(arr);`

`}`

Output:

``2``

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

## All You Need to Know About Dynamic Programming

In this article, I will introduce the concept of dynamic programming, developed by Richard Bellman in the 1950s, a powerful algorithm design technique to solve problems by breaking them down into smaller

## Maximize sum of pairwise products generated from the given Arrays

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Count of Ways to obtain given Sum from the given Array elements

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## How to Build a Dynamic Array By Using a Static Array in Java

We all know about the basic data structure, which is Array pretty well. And in java they are static. It means we have to allocate memory for the array ahead of time.

## Number of ways to select exactly K even numbers from given Array

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.