Minimum integer to be appended to convert given Array into equilibrium
Given an array A[] of length N, the task is to find the minimum integer to be appended at any end in the array to make it equilibrium.
An array is in equilibrium if there exists any index i such that:
sum of A[0, . . ., i-1] = sum of A[i+1, . . ., N-1]
Example:
Input: A[] = {5, 2, 6, 4, 3, 2}
Output: 2
Explanation: In the above array integer 2 is added at the front of the array then array become {2, 5, 2, 6, 4, 3, 2}.
Hence, index 3 is our equilibrium point.Input: A[] = {0, 6, 3, 4, 9}
Output: 0
Approach: The problem can be solved using two pointer approach as mentioned below:
- Keep left pointer at start and the right pointer at the end of the array
- Iterate the following steps till the left and right pointer become adjacent:
- If sum of array from start to left pointer ( = left_sum) is at least the sum of array from right pointer to end ( = right_sum), decrement the right pointer by 1
- else increment the left pointer by 1
- At the end, the absolute difference between the right sum and left sum will be the required minimum number to be added in the given such that the array stays in equilibrium,
Illustration:
Consider: A[] = {5, 2, 6, 4, 3, 2}
Initially, left_pointer will point at element 5 and right_pointer will point at element 2
- Since right_pointer is not adjacent to left_pointer, Iteration 1:
- left_sum = 5 and right_sum = 2,
- since left_sum ≥ right_sum, therefore shift right_pointer to 1 left (now at element 3)
- Since right_pointer is not adjacent to left_pointer, Iteration 2:
- left_sum = 5 and right_sum = 5,
- since left_sum = right_sum, therefore shift right_pointer to 1 left (now at element 4) and left_pointer to 1 right (now at element 2)
- Since right_pointer is not adjacent to left_pointer, Iteration 3:
- left_sum = 7 and right_sum = 9,
- since left_sum < right_sum, therefore shift left_pointer to 1 (now at element 6)
- Here in iteration 4, since right_pointer is adjacent to left_pointer, break the loop and go to next step
- Find the absolute difference between left_sum and right_sum = abs(9 – 7) = 2
Hence, 2 is the minimum number to be added to the array such that it stays in equilibrium.
Below is the implementation of the above approach:
C++
// C++ program of above approach. #include <bits/stdc++.h> using namespace std; // Function to find minimum integer // to be added either in front or at end // of the array to make it equilibrium int makeEquilibrium( int arr[], int n) { int i = 1, j = n - 2; // Initialize left and right sum with // first and last value of array int leftSum = arr[0], rightSum = arr[n - 1]; while (i <= j) { // If there is only one element present // between i and j, return // absolute value of left and right sum // which generate ans if (j - i < 1) return abs (leftSum - rightSum); // If left sum is less // increment i and add // element from front if (leftSum < rightSum) { leftSum += arr[i++]; } // If right sum is less // decrement j and add // element from end else if (leftSum > rightSum) { rightSum += arr[j--]; } // when both sum become equal else { leftSum += arr[i++]; rightSum += arr[j--]; } } } // Driver code int main() { int arr[] = { 5, 2, 6, 4, 3, 2 }; int N = sizeof (arr) / sizeof (arr[0]); cout << makeEquilibrium(arr, N); return 0; } |
Java
// JAVA program of above approach. import java.util.*; class GFG { // Function to find minimum integer // to be added either in front or at end // of the array to make it equilibrium public static int makeEquilibrium( int arr[], int n) { int i = 1 , j = n - 2 ; // Initialize left and right sum with // first and last value of array int leftSum = arr[ 0 ], rightSum = arr[n - 1 ]; while (i <= j) { // If there is only one element present // between i and j, return // absolute value of left and right sum // which generate ans if (j - i < 1 ) return Math.abs(leftSum - rightSum); // If left sum is less // increment i and add // element from front if (leftSum < rightSum) { leftSum += arr[i++]; } // If right sum is less // decrement j and add // element from end else if (leftSum > rightSum) { rightSum += arr[j--]; } // when both sum become equal else { leftSum += arr[i++]; rightSum += arr[j--]; } } return 0 ; } // Driver code public static void main(String[] args) { int arr[] = { 5 , 2 , 6 , 4 , 3 , 2 }; int N = arr.length; System.out.print(makeEquilibrium(arr, N)); } } // This code is contributed by Taranpreet |
Python3
# python3 program of above approach. # Function to find minimum integer # to be added either in front or at end # of the array to make it equilibrium def makeEquilibrium(arr, n): i, j = 1 , n - 2 # left and right sum with # first and last value of array leftSum, rightSum = arr[ 0 ], arr[n - 1 ] while (i < = j): # If there is only one element present # between i and j, return # absolute value of left and right sum # which generate ans if (j - i < 1 ): return abs (leftSum - rightSum) # If left sum is less # increment i and add # element from front if (leftSum < rightSum): leftSum + = arr[i] i + = 1 # If right sum is less # decrement j and add # element from end elif (leftSum > rightSum): rightSum + = arr[j] j - = 1 # when both sum become equal else : leftSum + = arr[i] i + = 1 rightSum + = arr[j] j - = 1 # Driver code if __name__ = = "__main__" : arr = [ 5 , 2 , 6 , 4 , 3 , 2 ] N = len (arr) print (makeEquilibrium(arr, N)) # This code is contributed by rakeshsahni |
C#
// C# program of above approach. using System; public class GFG{ // Function to find minimum integer // to be added either in front or at end // of the array to make it equilibrium static int makeEquilibrium( int [] arr, int n) { int i = 1, j = n - 2; // Initialize left and right sum with // first and last value of array int leftSum = arr[0], rightSum = arr[n - 1]; while (i <= j) { // If there is only one element present // between i and j, return // absolute value of left and right sum // which generate ans if (j - i < 1) return Math.Abs(leftSum - rightSum); // If left sum is less // increment i and add // element from front if (leftSum < rightSum) { leftSum += arr[i++]; } // If right sum is less // decrement j and add // element from end else if (leftSum > rightSum) { rightSum += arr[j--]; } // when both sum become equal else { leftSum += arr[i++]; rightSum += arr[j--]; } } return 0; } // Driver code static public void Main () { int [] arr = { 5, 2, 6, 4, 3, 2 }; int N = arr.Length; Console.Write(makeEquilibrium(arr, N)); } } // This code is contributed by hrithikgarg03188. |
Javascript
<script> // JavaScript program of above approach. // Function to find minimum integer // to be added either in front or at end // of the array to make it equilibrium const makeEquilibrium = (arr, n) => { let i = 1, j = n - 2; // Initialize left and right sum with // first and last value of array let leftSum = arr[0], rightSum = arr[n - 1]; while (i <= j) { // If there is only one element present // between i and j, return // absolute value of left and right sum // which generate ans if (j - i < 1) return Math.abs(leftSum - rightSum); // If left sum is less // increment i and add // element from front if (leftSum < rightSum) { leftSum += arr[i++]; } // If right sum is less // decrement j and add // element from end else if (leftSum > rightSum) { rightSum += arr[j--]; } // when both sum become equal else { leftSum += arr[i++]; rightSum += arr[j--]; } } } // Driver code let arr = [5, 2, 6, 4, 3, 2]; let N = arr.length; document.write(makeEquilibrium(arr, N)); // This code is contributed by rakeshsahni </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1).
Please Login to comment...