The Wayback Machine - https://web.archive.org/web/20221127223348/https://www.geeksforgeeks.org/minimize-absolute-sum-by-subtracting-one-and-two-from-adjacent-indices/
Skip to content
Related Articles

Related Articles

Minimize absolute Sum by subtracting one and two from adjacent indices

Improve Article
Save Article
  • Last Updated : 25 Nov, 2022
Improve Article
Save Article

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: Chose adjacent indices are 1, 2: {A1 – 1, A2 – 2, A3} = {3, 4, -1}
Second Operation: Chose adjacent indices are 1, 2: {A1 – 1, A2 – 2, A3} = {2, 2, -1}
Third Operation: Chose 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.

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);
    }
}

Output

12

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!