Techno Blender
Digitally Yours.
0 16

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

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

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

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

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

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