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 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[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++
// 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-increasingint 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 codeint main(){Â Â Â Â vector<int> arr = { 1, 5, 3, 9, 1 };Â Â Â Â cout << solve(arr);} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{Â
// Function to find the minimum operations// to make the array Non-increasingstatic 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 codepublic static void main(String[] args){Â Â Â Â int []arr = { 1, 5, 3, 9, 1 };Â Â Â Â System.out.print(solve(arr));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 program to implement# the above approachÂ
# Function to find the minimum operations # to make the array Non-increasingdef 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 Codearr = [ 1, 5, 3, 9, 1 ]Â
# Function callprint(solve(arr))Â
# This code is contributed by Shivam Singh |
C#
// C# program to implement// the above approachusing System;class GFG{Â
// Function to find the minimum operations// to make the array Non-increasingstatic 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 codepublic static void Main(String[] args){Â Â Â Â int []arr = { 1, 5, 3, 9, 1 };Â Â Â Â Console.Write(solve(arr));}}Â
// This code is contributed by PrinciRaj1992 |
Javascript
<script>Â
// Javascript program to implement// the above approachÂ
// Function to find the minimum operations// to make the array Non-increasingfunction solve(a){      // Size of the array    let n = a.length;      // Dp table initialization    let dp = new Array(n+1).fill(0);    let val = new Array(n+1).fill(0);      // dp[i]: Stores minimum number of    // operations required to make    // subarray {A[i], ..., A[N]} non-increasing    for (let i = n - 1; i >= 0; i--)    {        let sum = a[i];        let 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Â
    let arr = [ 1, 5, 3, 9, 1 ];    document.write(solve(arr));     </script> |
2
Â
Time Complexity: O(N2)Â
Auxiliary Space: O(N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



