Minimum pair merge operations required to make Array non-increasing

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:
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[0].
  • 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
close

play_arrow

link
brightness_4
code

// 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[0];
}
 
// Driver code
int main()
{
    vector<int> arr = { 1, 5, 3, 9, 1 };
    cout << solve(arr);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the minimum operations
// to make the array Non-increasing
static int solve(int []a)
{
 
    // Size of the array
    int n = a.length;
 
    // Dp table initialization
    int []dp = new int[n + 1];
    int []val = new int[n + 1];
 
    // 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--)
    {
        int sum = a[i];
        int j = i;
 
        while (j + 1 < n && 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[0];
}
 
// Driver code
public static void main(String[] args)
{
    int []arr = { 1, 5, 3, 9, 1 };
    System.out.print(solve(arr));
}
}
 
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the minimum operations
# to make the array Non-increasing
def solve(a):
 
    # Size of the array
    n = len(a)
 
    # Dp table initialization
    dp = [0] * (n + 1)
    val = [0] * (n + 1)
 
    # dp[i]: Stores minimum number of
    # operations required to make
    # subarray {A[i], ..., A[N]} non-increasing
    for i in range(n - 1, -1, -1):
        sum = a[i]
        j = i
 
        while(j + 1 < n and sum < val[j + 1]):
 
            # Increment the value of j
            j += 1
 
            # 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[0]
 
# Driver Code
arr = [ 1, 5, 3, 9, 1 ]
 
# Function call
print(solve(arr))
 
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the minimum operations
// to make the array Non-increasing
static int solve(int []a)
{
 
    // Size of the array
    int n = a.Length;
 
    // Dp table initialization
    int []dp = new int[n + 1];
    int []val = new int[n + 1];
 
    // 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--)
    {
        int sum = a[i];
        int j = i;
 
        while (j + 1 < n && 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[0];
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 5, 3, 9, 1 };
    Console.Write(solve(arr));
}
}
 
// This code is contributed by PrinciRaj1992

chevron_right


 
 

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.