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: Chose adjacent indices are 1, 2: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {3, 4, 1}
Second Operation: Chose adjacent indices are 1, 2: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {2, 2, 1}
Third Operation: Chose adjacent indices are 1, 2: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {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 nonoptimal 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[i1] =(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[i1]>0, then reduce arr[i] = 2 and arr[i1] =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

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: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {3, 4, 1}
Second Operation: Chose adjacent indices are 1, 2: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {2, 2, 1}
Third Operation: Chose adjacent indices are 1, 2: {A_{1 }– 1, A_{2 }– 2, A_{3}} = {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 nonoptimal 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[i1] =(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[i1]>0, then reduce arr[i] = 2 and arr[i1] =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

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