Minimize count of increments of each element of subarrays required to make Array non-increasing
Given an array arr[] consisting of N integers, the task is to find the minimum number of operations, which involves incrementing all elements of a subarray by 1, required to make the array non-increasing.
Examples:
Input: arr[] = {1, 3, 4, 1, 2}
Output: 4
Explanation:
In operation 1: Choose the subarray {1} and increase its value by 1. Now arr[] = {2, 3, 4, 1, 2}
In operation 2: Choose the subarray {2} and increase its value by 1. Now arr[] = {3, 3, 4, 1, 2}
In operation 3: Choose the subarray {3, 3} and increase its value by 1. Now arr[] = {4, 4, 4, 1, 2}
In operation 4: Choose the subarray {1} and increase its value by 1. Now arr[] = {4, 4, 4, 2, 2}
Thus, it took 4 operations to make the array non-increasing.
Input: arr[] = {10, 9, 8, 7, 7}
Output: 0
Explanation:
The array is already non-increasing
Approach: The approach is based on the fact that the operations can only be performed on subarrays. So only check for contiguous elements. Below are the steps:
- Otherwise, initialize a variable, say res, to store the count of operations required.
- Now, traverse the array and for each element, check if the element at index i is smaller than the element at index (i + 1). If found to be true, then add the difference between them to res, since both the elements need to be made equal to make the array non-increasing.
- Otherwise, move to the next element and repeat the above steps.
- Finally, after complete the traversal of the array, print the final value of res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMinOps( int arr[], int n)
{
int res = 0;
for ( int i = 0; i < n - 1; i++)
{
res += max(arr[i + 1] - arr[i], 0);
}
return res;
}
int main()
{
int arr[] = {1, 3, 4, 1, 2};
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMinOps(arr, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int getMinOps( int [] arr)
{
int res = 0 ;
for ( int i = 0 ;
i < arr.length - 1 ; i++)
{
res += Math.max(arr[i + 1 ]
- arr[i],
0 );
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 4 , 1 , 2 };
System.out.println(getMinOps(arr));
}
}
|
Python3
def getMinOps(arr):
res = 0
for i in range ( len (arr) - 1 ):
res + = max (arr[i + 1 ] - arr[i], 0 )
return res
arr = [ 1 , 3 , 4 , 1 , 2 ]
print (getMinOps(arr))
|
C#
using System;
class GFG{
public static int getMinOps( int [] arr)
{
int res = 0;
for ( int i = 0; i < arr.Length - 1; i++)
{
res += Math.Max(arr[i + 1] -
arr[i], 0);
}
return res;
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 4, 1, 2 };
Console.WriteLine(getMinOps(arr));
}
}
|
Javascript
<script>
function getMinOps(arr) {
var res = 0;
for (i = 0; i < arr.length - 1; i++) {
res += Math.max(arr[i + 1] - arr[i], 0);
}
return res;
}
var arr = [ 1, 3, 4, 1, 2 ];
document.write(getMinOps(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
16 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...