Decrease absolute Sum by subtracting one and two from adjoining indices

0
18


Given an arr[] of size N, the duty is to seek out the smallest attainable sum of the array after we can subtract 1 and a couple of respectively from two adjoining indices any variety of instances.

Examples:

Enter: N = 1, arr[] = { -45 }
Output: 45
Clarification: As there aren’t any consecutive indices current, Due to this fact, operation can’t be carried out, and the smallest attainable worth of absolute sum is 45.

Enter: N = 3, arr[] = {4,  6, -1}
Output: 2
Clarification:
First Operation: Selected adjoining indices are 1, 2: {A1 – 1, A2 – 2, A3} = {3, 4, -1}
Second Operation: Selected adjoining indices are 1, 2: {A1 – 1, A2 – 2, A3} = {2, 2, -1}
Third Operation: Selected adjoining indices are 1, 2: {A1 – 1, A2 – 2, A3} = {1, 0, -1}
Complete smallest attainable absolute sum: |1| + |0| + |-1| = 1 + 0 + 1 = 2.

Strategy: Implement the concept under to resolve the issue

The issue may be solved by traversing on arr[] utilizing two for loops one after the other and subtracting the values given in operations optimally 

It ought to be famous that the operation may be utilized on the identical indices till an non-optimal state of indices happen. Due to this fact, first we are going to attempt to cut back adjoining components utilizing operation a number of instances on the identical indices.    

  • First Loop:
    Traverse from final to first index of arr[], if a component discovered higher than 1, then cut back arr[i] -= 2 * (arr[i] / 2) and arr[i-1] -=(arr[i] / 2)
    It may be attainable that some adjoining components are nonetheless there on which operation may be carried out another time. So we are going to verify for these adjoining components in second loop.
  • Second Loop: Traverse from final to first index of arr[], if arr[i]>0 and arr[i-1]>0, then cut back arr[i] -= 2 and arr[i-1] -=1.

After making use of two above loops, Simply calculate absolutely the sum of components current in arr[] and print the sum.              

Observe the under steps to implement the concept:

  • Run a loop on arr[] from the final index to the primary index and apply the under idea:
    • If arr[i] > 1, Then set arr[i] -= 2 * (arr[i] / 2) and arr[i – 1] -= (arr[i] / 2).
  • Run one other loop on arr[] from the final index to the primary index and apply the under idea:
    • if arr[i] > 0 &&  arr[ i – 1] > 0, then cut back arr[i] -= 2 and arr[i – 1] -= 1.
  • Calculate absolutely the sum of the array and print the sum.

Under is the implementation of the above method.

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    

    public static void primary(String[] args)

    {

        int N = 5;

        int[] arr = { 6, -4, 3, 2, -1 };

  

        

        minAbsoluteSum(arr, N);

    }

  

    

    

    static void minAbsoluteSum(int arr[], int N)

    {

        

        

        lengthy sum = 0;

  

        

        

        

        

        for (int h = N - 1; h > 0; h--) {

            if (arr[h] > 1) {

                int instances = arr[h] / 2;

                arr[h] -= 2 * instances;

                arr[h - 1] -= instances;

            }

        }

  

        

        

        

        

        for (int okay = N - 1; okay > 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);

    }

}

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here