Count minimum decrement prefix or suffix or increment all operations to make Array equal to 0
Last Updated :
18 Aug, 2022
Given an array arr[] of size N. The task is to make all the array elements equal to zero by applying the minimum number of operations.
Following operations are allowed:
- Select an index i and decrease each element by 1 for the prefix up to that index.
- Select an index i and decrease each element by 1 for the suffix starting from that index.
- Increase all the elements of the array by 1.
Examples:
Input: arr[] = {2, 4, 6, 3, 7}
Output: Minimum operations: 12
Explanation: Select Index 0, and decrease all the elements from
index 1 to 4 by 2 in 2 operations, new arr[] would be {2, 2, 4, 1, 5}.
Select Index 1, and decrease all the elements from index 2 to 4
by 2 in 2 operations, new arr[] would be {2, 2, 2, -1, 3}
Select Index 3, and decrease all the elements from index 0 to 2
by 3 in 3 operations, new arr[] would be {-1, -1, -1, -1, 3}
Select Index 3, and decrease element of index 4 by 4 in 4 operations,
New arr[] would be {-1, -1, -1, -1, -1}
Increase all the elements by 1 in 1 operation,
Final arr[] would be {0, 0, 0, 0, 0}
Total, operations would be 2 + 2 + 3 + 4 + 1 = 12
Input: arr[] = {1, 3, 5, 7, 5, 2, 8}
Output: Minimum operations: 21
Approach:
The following problem can be solved using Greedy Approach.
Initially make all elements equal to the first element and then decrement them to have value as 0.
To do this following steps can be taken:
- For making all the elements equal to the first element, we will use the difference between the consecutive elements. ( arr[i+1] – arr[i] ).
- If the difference (diff = arr[i+1] – arr[i]) is positive, decrease the elements of suffix starting from index (i+1).
- Number of operations required = abs( diff )
- If the difference (diff = arr[i+1] – arr[i]) is negative, decrease the elements of prefix up to index i.
- Number of operations required = abs( diff ) . Decreasing the prefix will also decrease the first element of the array by abs( diff).
- Finally, the total number of required operations will be operations calculated through the absolute difference between the consecutive elements of the array plus the absolute value of the first element of the array.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int minimumOperations( int arr[], int n)
{
int i;
int operations = 0;
for ( int i = 0; i < n - 1; i++) {
operations += abs (arr[i + 1] - arr[i]);
if (arr[i + 1] - arr[i] < 0) {
arr[0] -= ( abs (arr[i + 1] - arr[i]));
}
}
operations += abs (arr[0]);
return operations;
}
int main()
{
int arr[] = { 2, 4, 6, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << "Minimum operations: "
<< minimumOperations(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minimumOperations( int arr[], int n)
{
int i;
int operations = 0 ;
for (i = 0 ; i < n - 1 ; i++) {
operations += Math.abs(arr[i + 1 ] - arr[i]);
if (arr[i + 1 ] - arr[i] < 0 ) {
arr[ 0 ] -= (Math.abs(arr[i + 1 ] - arr[i]));
}
}
operations += Math.abs(arr[ 0 ]);
return operations;
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 6 , 3 , 7 };
int N = arr.length;
System.out.print( "Minimum operations: "
+ minimumOperations(arr, N));
}
}
|
Python3
def minimumOperations(arr, n):
i = 0
operations = 0
for i in range (n - 1 ):
operations + = abs (arr[i + 1 ] - arr[i])
if (arr[i + 1 ] - arr[i]) < 0 :
arr[ 0 ] - = ( abs (arr[i + 1 ] - arr[i]))
operations + = abs (arr[ 0 ])
return operations
if __name__ = = "__main__" :
arr = [ 2 , 4 , 6 , 3 , 7 ]
N = 5
print ( "Minimum operations: " , minimumOperations(arr, N))
|
Javascript
<script>
function minimumOperations(arr,n)
{
let i;
let operations = 0;
for (let i = 0; i < n - 1; i++) {
operations += Math.abs(arr[i + 1] - arr[i]);
if (arr[i + 1] - arr[i] < 0) {
arr[0] -= (Math.abs(arr[i + 1] - arr[i]));
}
}
operations += Math.abs(arr[0]);
return operations;
}
let arr = [ 2, 4, 6, 3, 7 ];
let N = arr.length;
document.write( "Minimum operations: "
+ minimumOperations(arr, N));
</script>
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int minimumOperations( int [] arr, int n)
{
int i;
int operations = 0;
for (i = 0; i < n - 1; i++) {
operations += Math.Abs(arr[i + 1] - arr[i]);
if (arr[i + 1] - arr[i] < 0) {
arr[0] -= (Math.Abs(arr[i + 1] - arr[i]));
}
}
operations += Math.Abs(arr[0]);
return operations;
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 6, 3, 7 };
int N = arr.Length;
Console.WriteLine( "Minimum operations: "
+ minimumOperations(arr, N));
}
}
|
Output
Minimum operations: 12
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...