 GFG App
Open App Browser
Continue

# Minimize absolute Sum by subtracting one and two from adjacent indices

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++

```// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;

// Function to calculate minimum absolute
// possible value
void minAbsoluteSum(int arr[], int N)
{
// Long variable to calculate absolute
// sum of arr[]
long sum = 0;

// First loop for traversing on arr[]
// and subtracting values of given
// operation if element greater than
// 1 is present
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;
}
}

// Second loop for traversing on arr[]
// and subtracting values of given
// operation if adjacent element greater
// than 0 are present
for (int k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}

// Loop for calculating absolute sum
for (int l = 0; l < N; l++) {
sum += abs(arr[l]);
}

// Printing total smallest possible
// absolute sum
cout << sum;
}

// Driver code
int main()
{
int N = 5;
int arr[] = { 6, -4, 3, 2, -1 };

// Function call
minAbsoluteSum(arr, N);
}

// This code is contributed by Samim Hossain Mondal.```

## Java

```// Java code to implement the approach

import java.io.*;
import java.lang.*;
import java.util.*;

class GFG {

// Driver code
public static void main(String[] args)
{
int N = 5;
int[] arr = { 6, -4, 3, 2, -1 };

// Function call
minAbsoluteSum(arr, N);
}

// Function to calculate minimum absolute
// possible value
static void minAbsoluteSum(int arr[], int N)
{
// Long variable to calculate absolute
// sum of arr[]
long sum = 0;

// First loop for traversing on arr[]
// and subtracting values of given
// operation if element greater than
// 1 is present
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;
}
}

// Second loop for traversing on arr[]
// and subtracting values of given
// operation if adjacent element greater
// than 0 are present
for (int k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}

// Loop for calculating absolute sum
for (int l = 0; l < N; l++) {
sum += Math.abs(arr[l]);
}

// Printing total smallest possible
// absolute sum
System.out.println(sum);
}
}```

## Python3

```# Python code to implement the approach

# Function to calculate minimum absolute
# possible value
def minAbsoluteSum(arr, N):

# Long variable to calculate absolute
# sum of arr[]
summ = 0

# First loop for traversing on arr[]
# and subtracting values of given
# operation if element greater than
# 1 is present
# for ( h = N - 1 h > 0 h--)
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

# Second loop for traversing on arr[]
# and subtracting values of given
# operation if adjacent element greater
# than 0 are present
for k in range(N-1, 0, -1):
if (arr[k] > 0 and arr[k - 1] > 0):
arr[k] -= 2
arr[k - 1] -= 1
# Loop for calculating absolute sum
for l in range(0, N):
summ += abs(arr[l])
# Printing total smallest possible
# absolute sum
print(summ)

# Driver code
N = 5
arr = [6, -4, 3, 2, -1]
# Function call
minAbsoluteSum(arr, N)

# This code is contributed by ksam24000
```

## C#

```// C# code to implement the approach
using System;
public class GFG {

// Function to calculate minimum absolute
// possible value
static void minAbsoluteSum(int[] arr, int N)
{

// Long variable to calculate absolute
// sum of arr[]
long sum = 0;

// First loop for traversing on arr[]
// and subtracting values of given
// operation if element greater than
// 1 is present
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;
}
}

// Second loop for traversing on arr[]
// and subtracting values of given
// operation if adjacent element greater
// than 0 are present
for (int k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}

// Loop for calculating absolute sum
for (int l = 0; l < N; l++) {
sum += Math.Abs(arr[l]);
}

// Printing total smallest possible
// absolute sum
Console.WriteLine(sum);
}

static public void Main()
{

// Code
int N = 5;
int[] arr = { 6, -4, 3, 2, -1 };

// Function call
minAbsoluteSum(arr, N);
}
}

// This code is contributed by lokeshmvs21.```

## Javascript

```        // JavaScript code to implement the approach

// Function to calculate minimum absolute
// possible value
const minAbsoluteSum = (arr, N) => {
// Long variable to calculate absolute
// sum of arr[]
let sum = 0;

// First loop for traversing on arr[]
// and subtracting values of given
// operation if element greater than
// 1 is present
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;
}
}

// Second loop for traversing on arr[]
// and subtracting values of given
// operation if adjacent element greater
// than 0 are present
for (let k = N - 1; k > 0; k--) {
if (arr[k] > 0 && arr[k - 1] > 0) {
arr[k] -= 2;
arr[k - 1]--;
}
}

// Loop for calculating absolute sum
for (let l = 0; l < N; l++) {
sum += Math.abs(arr[l]);
}

// Printing total smallest possible
// absolute sum
console.log(sum);
}

// Driver code

let N = 5;
let arr = [6, -4, 3, 2, -1];

// Function call
minAbsoluteSum(arr, N);

// This code is contributed by rakeshsahni
```
Output

`12`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up