Smallest Integer to be inserted to have equal sums

Given an array of positive integers, find the smallest non-negative integer (i.e. greater than or equal to zero) that can be placed between any two elements of the array such that the sum of elements in the subarray occurring before it is equal to the sum of elements occurring in the subarray after it, with the newly placed integer included in either of the two subarrays.

Examples:

Input : arr = {3, 2, 1, 5, 7, 8}
Output : 4
Explanation : The smallest possible number that we can insert is 4, at position 5 (i.e. between 5 and 7) as part of the first subarray so that the sum of the two subarrays becomes equal as, 3+2+1+5+4=15 and 7+8=15.

Input : arr = {3, 2, 2, 3}
Output : 0
Explanation: Equal sum of 5 is obtained by adding first two elements and last two elements as separate subarrays without inserting any extra number. Hence, the smallest integer to be inserted is 0 at position 3.

In order to split the array in such a way that it gives two subarrays with equal sums of their respective elements, a very simple and straightforward approach is to keep adding elements from the beginning of the array, one by one and find the difference between their sum and sum of rest of the elements. We use an iterative loop to do so. For array indexes 0 to N-1, we keep adding elements from left to right and find its difference with the remaining sum. During the first iteration, the difference obtained is considered to be minimum in order to carry out further comparisons. For the rest of the iterations, if the difference obtained is less than the minimum considered earlier, we update the value of minimum. Till the end of the loop, we finally obtain the minimum number that can be added.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the smallest 
// number to be added to make the 
// sum of left and right subarrays equal
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the minimum
// value to be added
int findMinEqualSums(int a[], int N)
{
    // Variable to store entire 
    // array sum
    int sum = 0;
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
    }
  
    // Variables to store sum of
    // subarray1 and subarray 2
    int sum1 = 0, sum2 = 0;
  
    // minimum value to be added
    int min = INT_MAX;
  
    // Traverse through the array
    for (int i = 0; i < N; i++) 
    {
        // Sum of both halves
        sum1 += a[i];
        sum2 = sum - sum1;
  
        // Calculate minimum number 
        // to be added
        if (abs(sum1 - sum2) < min)
        {
            min = abs(sum1 - sum2);
        }
  
        if (min == 0) 
        {
            break;
        }
    }
  
    return min;
}
  
// Driver code
int main()
{
    int a[] = { 3, 2, 1, 5, 7, 8 };
  
    // Length of array
    int N = sizeof(a) / sizeof(a[0]);
  
    cout << (findMinEqualSums(a, N));
}
  
// This code is contributed
// by ChitraNayal

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the smallest 
// number to be added to make the 
// sum of left and right subarrays equal
import java.io.*;
import java.util.*;
  
class GFG 
{
  
// Function to find the minimum 
// value to be added
static int findMinEqualSums(int[] a, int N)
{
    // Variable to store 
    // entire array sum
    int sum = 0;
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
    }
  
    // Variables to store sum of
    // subarray1 and subarray 2
    int sum1 = 0, sum2 = 0;
  
    // minimum value to be added
    int min = Integer.MAX_VALUE;
  
    // Traverse through the array
    for (int i = 0; i < N; i++)
    {
        // Sum of both halves
        sum1 += a[i];
        sum2 = sum - sum1;
  
        // Calculate minimum number 
        // to be added
        if (Math.abs(sum1 - sum2) < min) 
        {
            min = Math.abs(sum1 - sum2);
        }
  
        if (min == 0
        {
            break;
        }
    }
  
    return min;
}
  
// Driver code
public static void main(String args[])
{
    int[] a = { 3, 2, 1, 5, 7, 8 };
  
    // Length of array
    int N = a.length;
  
    System.out.println(findMinEqualSums(a, N));
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import sys
# Python3 program to find the smallest 
# number to be added to make the 
# sum of left and right subarrays equal
  
# Function to find the minimum
# value to be added
def findMinEqualSums(a, N):
  
    # Variable to store entire 
    # array sum
    sum = 0
    for i in range(0,N): 
      
        sum = sum+a[i]
      
  
    # Variables to store sum of
    # subarray1 and subarray 2
    sum1 = 0
    sum2 = 0
  
    # minimum value to be added
    min = sys.maxsize
  
    # Traverse through the array
    for i in range(0, N-1): 
      
        # Sum of both halves
        sum1 += a[i]
        sum2 = sum - sum1
  
        # Calculate minimum number 
        # to be added
        if (abs(sum1 - sum2) < min):
            min = abs(sum1 - sum2)
          
  
        if (min == 0) :
          
            break
    return min
  
# Driver code
if __name__=='__main__':
    a = [3, 2, 1, 5, 7, 8]
  
    # Length of array
    N = len(a) 
  
    print(findMinEqualSums(a, N))
  
# This code is contributed
# by Shivi_Aggarwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the smallest 
// number to be added to make the 
// sum of left and right subarrays equal
using System;
class GFG 
{
  
// Function to find the minimum 
// value to be added
static int findMinEqualSums(int[] a, int N)
{
    // Variable to store 
    // entire array sum
    int sum = 0;
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
    }
  
    // Variables to store sum of
    // subarray1 and subarray 2
    int sum1 = 0, sum2 = 0;
  
    // minimum value to be added
    int min = int.MaxValue;
  
    // Traverse through the array
    for (int i = 0; i < N; i++)
    {
        // Sum of both halves
        sum1 += a[i];
        sum2 = sum - sum1;
  
        // Calculate minimum number 
        // to be added
        if (Math.Abs(sum1 - sum2) < min) 
        {
            min = Math.Abs(sum1 - sum2);
        }
  
        if (min == 0) 
        {
            break;
        }
    }
  
    return min;
}
  
// Driver code
public static void Main()
{
    int[] a = { 3, 2, 1, 5, 7, 8 };
  
    // Length of array
    int N = a.Length;
  
    Console.WriteLine(findMinEqualSums(a, N));
}
}
// This code is contributed by shs

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the smallest 
// number to be added to make the 
// sum of left and right subarrays equal
  
// Function to find the minimum
// value to be added
function findMinEqualSums($a, $N)
{
    // Variable to store entire 
    // array sum
    $sum = 0;
    for ($i = 0; $i < $N; $i++) 
    {
        $sum += $a[$i];
    }
  
    // Variables to store sum of
    // subarray1 and subarray 2
    $sum1 = 0; $sum2 = 0;
  
    // minimum value to be added
    $min = PHP_INT_MAX;
  
    // Traverse through the array
    for ($i = 0; $i < $N; $i++) 
    {
        // Sum of both halves
        $sum1 += $a[$i];
        $sum2 = $sum - $sum1;
  
        // Calculate minimum number 
        // to be added
        if (abs($sum1 - $sum2) < $min)
        {
            $min = abs($sum1 - $sum2);
        }
  
        if ($min == 0) 
        {
            break;
        }
    }
  
    return $min;
}
  
// Driver code
$a = array( 3, 2, 1, 5, 7, 8 );
  
// Length of array
$N = count($a);
  
echo (findMinEqualSums($a, $N));
  
// This code is contributed
// shs
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.