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-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); } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Javascript program to implement // the above approach // Function to find the minimum operations // to make the array Non-increasing function 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 neveropen!