Minimize absolute Sum by subtracting one and two from adjacent indices
Last Updated :
20 Dec, 2022
Given an arr[] of length N, the task is to find the smallest possible sum of the array when we can subtract 1 and 2 respectively from two adjacent indices any number of times.
Examples:
Input: N = 1, arr[] = { -45 }
Output: 45
Explanation: As there are no consecutive indices present, Therefore, operation can’t be performed, and the smallest possible value of absolute sum is 45.
Input: N = 3, arr[] = {4, 6, -1}
Output: 2
Explanation:
First Operation: Choose adjacent indices are 1, 2: {A1 – 1, A2 – 2, A3} = {3, 4, -1}
Second Operation: Choose adjacent indices are 1, 2: {A1 – 1, A2 – 2, A3} = {2, 2, -1}
Third Operation: Choose adjacent indices are 1, 2: {A1 – 1, A2 – 2, A3} = {1, 0, -1}
Total smallest possible absolute sum: |1| + |0| + |-1| = 1 + 0 + 1 = 2.
Approach: Implement the idea below to solve the problem
The problem can be solved by traversing on arr[] using two for loops one by one and subtracting the values given in operations optimally
It should be noted that the operation can be applied on the same indices until an non-optimal state of indices occur. Therefore, first we will try to reduce adjacent elements using operation multiple times at the same indices.
- First Loop:
Traverse from last to first index of arr[], if an element found greater than 1, then reduce arr[i] -= 2 * (arr[i] / 2) and arr[i-1] -=(arr[i] / 2)
It can be possible that some adjacent elements are still there on which operation can be performed one more time. So we will check for those adjacent elements in second loop.
- Second Loop: Traverse from last to first index of arr[], if arr[i]>0 and arr[i-1]>0, then reduce arr[i] -= 2 and arr[i-1] -=1.
After applying two above loops, Just calculate the absolute sum of elements present in arr[] and print the sum.
Follow the below steps to implement the idea:
- Run a loop on arr[] from the last index to the first index and apply the below concept:
- If arr[i] > 1, Then set arr[i] -= 2 * (arr[i] / 2) and arr[i – 1] -= (arr[i] / 2).
- Run another loop on arr[] from the last index to the first index and apply the below concept:
- if arr[i] > 0 && arr[ i – 1] > 0, then reduce arr[i] -= 2 and arr[i – 1] -= 1.
- Calculate the absolute sum of the array and print the sum.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void minAbsoluteSum( int arr[], int N)
{
long sum = 0;
for ( int h = N - 1; h > 0; h--) {
if (arr[h] > 1) {
int times = arr[h] / 2;
arr[h] -= 2 * times;
arr[h - 1] -= times;
}
}
for ( int k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}
for ( int l = 0; l < N; l++) {
sum += abs (arr[l]);
}
cout << sum;
}
int main()
{
int N = 5;
int arr[] = { 6, -4, 3, 2, -1 };
minAbsoluteSum(arr, N);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 5 ;
int [] arr = { 6 , - 4 , 3 , 2 , - 1 };
minAbsoluteSum(arr, N);
}
static void minAbsoluteSum( int arr[], int N)
{
long sum = 0 ;
for ( int h = N - 1 ; h > 0 ; h--) {
if (arr[h] > 1 ) {
int times = arr[h] / 2 ;
arr[h] -= 2 * times;
arr[h - 1 ] -= times;
}
}
for ( int k = N - 1 ; k > 0 ; k--) {
if (arr[k] > 0 && arr[k - 1 ] > 0 ) {
arr[k] -= 2 ;
arr[k - 1 ]--;
}
}
for ( int l = 0 ; l < N; l++) {
sum += Math.abs(arr[l]);
}
System.out.println(sum);
}
}
|
Python3
def minAbsoluteSum(arr, N):
summ = 0
for h in range (N - 1 , 0 , - 1 ):
if (arr[h] > 1 ):
times = ( int )(arr[h] / 2 )
arr[h] - = 2 * times
arr[h - 1 ] - = times
for k in range (N - 1 , 0 , - 1 ):
if (arr[k] > 0 and arr[k - 1 ] > 0 ):
arr[k] - = 2
arr[k - 1 ] - = 1
for l in range ( 0 , N):
summ + = abs (arr[l])
print (summ)
N = 5
arr = [ 6 , - 4 , 3 , 2 , - 1 ]
minAbsoluteSum(arr, N)
|
C#
using System;
public class GFG {
static void minAbsoluteSum( int [] arr, int N)
{
long sum = 0;
for ( int h = N - 1; h > 0; h--) {
if (arr[h] > 1) {
int times = arr[h] / 2;
arr[h] -= 2 * times;
arr[h - 1] -= times;
}
}
for ( int k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}
for ( int l = 0; l < N; l++) {
sum += Math.Abs(arr[l]);
}
Console.WriteLine(sum);
}
static public void Main()
{
int N = 5;
int [] arr = { 6, -4, 3, 2, -1 };
minAbsoluteSum(arr, N);
}
}
|
Javascript
const minAbsoluteSum = (arr, N) => {
let sum = 0;
for (let h = N - 1; h > 0; h--) {
if (arr[h] > 1) {
let times = arr[h] / 2;
arr[h] -= 2 * times;
arr[h - 1] -= times;
}
}
for (let k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}
for (let l = 0; l < N; l++) {
sum += Math.abs(arr[l]);
}
console.log(sum);
}
let N = 5;
let arr = [6, -4, 3, 2, -1];
minAbsoluteSum(arr, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...