Minimize subarray increments/decrements required to reduce all array elements to 0
Last Updated :
16 Apr, 2021
Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray:
- Increment by one
- Decrement by one
The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0.
Examples:
Input: arr[] = {1, 3, 4, 1}
Output: 4
Explanation:
Optimal steps to reduce all array elements to 0 are as follows:
Step 1: Select subarray [1, 3, 4, 1] and convert it to [0, 2, 3, 0], by decrementing each element by 1
Array modifies to {0, 2, 3, 0}
Step 2: Select subarray [2, 3] and convert it to [1, 2], by decrementing each element by 1
Array modifies to {0, 1, 2, 0}
Step 3: Select subarray [1, 2] and convert it to [0, 1], by decrementing each element by 1
Array modifies to {0, 0, 1, 0}
Step 4: Select subarray [1] convert it to [0]
Array modifies to {0, 0, 0, 0}
Therefore, minimum number of steps required is 4.
Input: arr[] = {-2, 0, -3, 1, 2}
Output: 5
Explanation:
Optimal steps to reduce all array elements to 0 are as follows:
Step 1: Select subarray [-2, 0, -3] and convert it to [-1, 0, -2], by incrementing each element by 1 except 0.
Array modifies to {-1, 0, -2, 1, 2}
Step 2: Select subarray [-1, 0, -2] and convert it to [0, 0, -1], by incrementing each element by 1 except 0.
Array modifies to {0, 0, -1, 1, 2}
Step 3: Select subarray [-1] convert it to [0]
Array modifies to {0, 0, 0, 1, 2}
Step 4: Select subarray [1, 2] and convert it to [0, 1], by decrementing each element by 1
Array modifies to {0, 0, 0, 0, 1}
Step 5: Select [1] convert it to [0]
Array modifies to {0, 0, 0, 0, 0}
Therefore, minimum number of steps required is 5
Approach: To solve the problem, traverse the array and find the minimum negative number and the maximum positive number. The sum of their absolute values is the minimum number of operations required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperation( int arr[], int N)
{
int minOp = INT_MIN;
int minNeg = 0, maxPos = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] < 0)
{
if (arr[i] < minNeg)
minNeg = arr[i];
}
else
{
if (arr[i] > maxPos)
maxPos = arr[i];
}
}
return abs (minNeg) + maxPos;
}
int main()
{
int arr[] = {1, 3, 4, 1};
int N = sizeof (arr) / sizeof (arr[0]);
cout << minOperation(arr, N);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int minOperation( int [] arr)
{
int minOp = Integer.MIN_VALUE;
int minNeg = 0 , maxPos = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] < 0 ) {
if (arr[i] < minNeg)
minNeg = arr[i];
}
else {
if (arr[i] > maxPos)
maxPos = arr[i];
}
}
return Math.abs(minNeg) + maxPos;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 4 , 1 };
System.out.println(minOperation(arr));
}
}
|
Python3
import sys
def minOperation(arr):
minOp = sys.maxsize
minNeg = 0
maxPos = 0
for i in range ( len (arr)):
if (arr[i] < 0 ):
if (arr[i] < minNeg):
minNeg = arr[i]
else :
if arr[i] > maxPos:
maxPos = arr[i]
return abs (minNeg) + maxPos
if __name__ = = "__main__" :
arr = [ 1 , 3 , 4 , 1 ]
print (minOperation(arr))
|
C#
using System;
class GFG{
static int minOperation( int [] arr)
{
int minOp = int .MinValue;
int minNeg = 0, maxPos = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] < 0)
{
if (arr[i] < minNeg)
minNeg = arr[i];
}
else
{
if (arr[i] > maxPos)
maxPos = arr[i];
}
}
return Math.Abs(minNeg) + maxPos;
}
public static void Main(String[] args)
{
int []arr = {1, 3, 4, 1};
Console.WriteLine(minOperation(arr));
}
}
|
Javascript
<script>
function minOperation(arr)
{
let minOp = Number.MIN_VALUE;
let minNeg = 0, maxPos = 0;
for (let i = 0; i < arr.length; i++)
{
if (arr[i] < 0)
{
if (arr[i] < minNeg)
minNeg = arr[i];
}
else
{
if (arr[i] > maxPos)
maxPos = arr[i];
}
}
return Math.abs(minNeg) + maxPos;
}
let arr = [ 1, 3, 4, 1 ];
document.write(minOperation(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...