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++
#include <bits/stdc++.h>
using namespace std;
int solve(vector< int >& a)
{
int n = a.size();
vector< int > dp(n + 1, 0), val(n + 1, 0);
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]) {
j++;
sum += a[j];
}
dp[i] = (j - i) + dp[j + 1];
val[i] = sum;
}
return dp[0];
}
int main()
{
vector< int > arr = { 1, 5, 3, 9, 1 };
cout << solve(arr);
}
|
Java
import java.util.*;
class GFG{
static int solve( int []a)
{
int n = a.length;
int []dp = new int [n + 1 ];
int []val = new int [n + 1 ];
for ( int i = n - 1 ; i >= 0 ; i--)
{
int sum = a[i];
int j = i;
while (j + 1 < n && sum < val[j + 1 ])
{
j++;
sum += a[j];
}
dp[i] = (j - i) + dp[j + 1 ];
val[i] = sum;
}
return dp[ 0 ];
}
public static void main(String[] args)
{
int []arr = { 1 , 5 , 3 , 9 , 1 };
System.out.print(solve(arr));
}
}
|
Python3
def solve(a):
n = len (a)
dp = [ 0 ] * (n + 1 )
val = [ 0 ] * (n + 1 )
for i in range (n - 1 , - 1 , - 1 ):
sum = a[i]
j = i
while (j + 1 < n and sum < val[j + 1 ]):
j + = 1
sum + = a[j]
dp[i] = (j - i) + dp[j + 1 ]
val[i] = sum
return dp[ 0 ]
arr = [ 1 , 5 , 3 , 9 , 1 ]
print (solve(arr))
|
C#
using System;
class GFG{
static int solve( int []a)
{
int n = a.Length;
int []dp = new int [n + 1];
int []val = new int [n + 1];
for ( int i = n - 1; i >= 0; i--)
{
int sum = a[i];
int j = i;
while (j + 1 < n && sum < val[j + 1])
{
j++;
sum += a[j];
}
dp[i] = (j - i) + dp[j + 1];
val[i] = sum;
}
return dp[0];
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 3, 9, 1 };
Console.Write(solve(arr));
}
}
|
Javascript
<script>
function solve(a)
{
let n = a.length;
let dp = new Array(n+1).fill(0);
let val = new Array(n+1).fill(0);
for (let i = n - 1; i >= 0; i--)
{
let sum = a[i];
let j = i;
while (j + 1 < n && sum < val[j + 1])
{
j++;
sum += a[j];
}
dp[i] = (j - i) + dp[j + 1];
val[i] = sum;
}
return dp[0];
}
let arr = [ 1, 5, 3, 9, 1 ];
document.write(solve(arr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)