Given an array of even size, task is to find minimum value that can be added to an element so that array become balanced. An array is balanced if the sum of the left half of the array elements is equal to the sum of right half. Suppose, we have an array 1 3 1 2 4 3. The Sum of first three elements is 1 + 3 + 1 = 5 and sum of last three elements is 2 + 4 + 3 = 9
So this is unbalanced, to make it balanced the minimum number we can add is 4 to any element in first half.
Input : 1 2 1 2 1 3 Output : 2 Sum of first 3 elements is 1 + 2 + 1 = 4, sum of last three elements is 2 + 1 + 3 = 6 To make the array balanced you can add 2. Input : 20 10 Output : 10
The idea is simple, we compute sums of first and second halves. Once these sums are computed, we return absolute difference of these two values.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find whether an array is subset of another array | Added Method 3
- Find minimum value to assign all array elements so that array product becomes greater
- Find the repeating and the missing | Added 3 new methods
- Find the first, second and third minimum elements in an array
- Find the minimum value from an array associated with another array
- Find the integers that doesnot ends with T1 or T2 when squared and added X
- Check if array elements are consecutive | Added Method 3
- Find minimum adjustment cost of an array
- Find minimum length sub-array which has given sub-sequence in it
- Find minimum in an array without using Relational Operators
- k largest(or smallest) elements in an array | added Min Heap method
- Find k ordered pairs in array with minimum difference d
- Find the K-th minimum element from an array concatenated M times
- Find the minimum element in a sorted and rotated array
- Find the minimum number of moves to reach end of the array
Improved By : nitin mittal