Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Find the minimum value to be added so that array becomes balanced

  • Difficulty Level : Basic
  • Last Updated : 01 Mar, 2021

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.
Examples : 
 

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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. 
 



C++




#include <bits/stdc++.h>
using namespace std;
 
// Returns minimum value that need to be added
// to make array balanced.
int minValueToBalance(int a[], int n)
{
    // Calculating sum of first half elements
    // of an array
    int sum1 = 0;
    for (int i = 0; i < n/2; i++)
        sum1 += a[i];
 
    // Calculating sum of other half elements
    // of an array
    int sum2 = 0;
    for (int i = n/2; i < n; i++)
        sum2 += a[i];
 
    // calculating difference
    return abs(sum1 - sum2);
}
 
// Driver code
int main()
{
    int arr[] = {1, 7, 1, 1, 3, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << minValueToBalance(arr, n)<<endl;
    return 0;
}

Java




// Java program to Find the minimum value
// to be added so that array becomes balanced
 
class Minimum
{
    // Returns minimum value that need to
    // be added to make array balanced.
    public static int minValueToBalance(int a[],
                                        int n)
    {
        // Calculating sum of first half
        // elements of an array
        int sum1 = 0;
        for (int i = 0; i < n / 2; i++)
            sum1 += a[i];
 
        // Calculating sum of other half
        // elements of an array
        int sum2 = 0;
        for (int i = n/2; i < n; i++)
            sum2 += a[i];
 
        // calculating difference
        return Math.abs(sum1 - sum2);
    }
     
    // driver code
    public static void main(String[] args)
    {
        int arr[] = {1, 7, 1, 1, 3, 1};
        int n = 6;
        System.out.print(minValueToBalance(arr, n));
    }
}
 
// This code is contributed by rishabh_jain

Python3




# Python3 program to Find the
# minimum value to be added so that
# array becomes balanced
 
# Returns minimum value that need to
# be added to make array balanced.
def minValueToBalance(a, n):
     
    # Calculating sum of first
    # half elements of an array
    sum1 = 0
    for i in range( int(n / 2)):
        sum1 += a[i]
         
    # Calculating sum of other
    # half elements of an array
    sum2 = 0;
    i = int(n / 2)
    while i < n:
        sum2 += a[i]
        i = i + 1
     
    # calculating difference
    return abs(sum1 - sum2)
 
# Driver code
arr = [1, 7, 1, 1, 3, 1]
n = len(arr)
print(minValueToBalance(arr, n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#




// C# program to Find the minimum value
// to be added so that array becomes balanced
using System;
 
class Minimum {
     
    // Returns minimum value that need to
    // be added to make array balanced.
    public static int minValueToBalance(int []a,
                                        int n)
    {
         
        // Calculating sum of first half
        // elements of an array
        int sum1 = 0;
        for (int i = 0; i < n / 2; i++)
            sum1 += a[i];
 
        // Calculating sum of other half
        // elements of an array
        int sum2 = 0;
        for (int i = n / 2; i < n; i++)
            sum2 += a[i];
 
        // calculating difference
        return Math.Abs(sum1 - sum2);
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = {1, 7, 1, 1, 3, 1};
        int n = 6;
        Console.Write(minValueToBalance(arr, n));
    }
}
 
// This code is contributed by Nitin Mittal.

PHP




<?php
// Returns minimum value
// that need to be added
// to make array balanced.
function minValueToBalance($a, $n)
{
    // Calculating sum of first
    // half elements of an array
    $sum1 = 0;
    for ($i = 0; $i < $n / 2; $i++)
        $sum1 += $a[$i];
 
    // Calculating sum of other
    // half elements of an array
    $sum2 = 0;
    for ( $i = $n / 2; $i < $n; $i++)
        $sum2 += $a[$i];
 
    // calculating difference
    return abs($sum1 - $sum2);
}
 
// Driver code
$arr = array(1, 7, 1, 1, 3, 1);
$n = sizeof($arr) / sizeof($arr[0]);
echo minValueToBalance($arr, $n);
 
// This code is contributed
// by nitin mittal.
?>

Javascript




<script>
 
// Returns minimum value that need to be added
// to make array balanced.
function minValueToBalance(a, n)
{
    // Calculating sum of first half elements
    // of an array
    let sum1 = 0;
    for (let i = 0; i < Math.floor(n/2); i++)
        sum1 += a[i];
 
    // Calculating sum of other half elements
    // of an array
    let sum2 = 0;
    for (let i = Math.floor(n/2); i < n; i++)
        sum2 += a[i];
 
    // calculating difference
    return Math.abs(sum1 - sum2);
}
 
// Driver code
    let arr = [1, 7, 1, 1, 3, 1];
    let n = arr.length;
    document.write(minValueToBalance(arr, n) + "<br>");
 
// This code is contributed by Surbhi Tyagi.
 
</script>

Output : 

4

 

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 contribute@geeksforgeeks.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.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :