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:
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[0];
}
// 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.
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
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.
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.
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.
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.