Smallest Integer to be inserted to have equal sums
Last Updated :
05 Sep, 2022
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++
#include<bits/stdc++.h>
using namespace std;
int findMinEqualSums( int a[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += a[i];
}
int sum1 = 0, sum2 = 0;
int min = INT_MAX;
for ( int i = 0; i < N; i++)
{
sum1 += a[i];
sum2 = sum - sum1;
if ( abs (sum1 - sum2) < min)
{
min = abs (sum1 - sum2);
}
if (min == 0)
{
break ;
}
}
return min;
}
int main()
{
int a[] = { 3, 2, 1, 5, 7, 8 };
int N = sizeof (a) / sizeof (a[0]);
cout << (findMinEqualSums(a, N));
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int findMinEqualSums( int [] a, int N)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += a[i];
}
int sum1 = 0 , sum2 = 0 ;
int min = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++)
{
sum1 += a[i];
sum2 = sum - sum1;
if (Math.abs(sum1 - sum2) < min)
{
min = Math.abs(sum1 - sum2);
}
if (min == 0 )
{
break ;
}
}
return min;
}
public static void main(String args[])
{
int [] a = { 3 , 2 , 1 , 5 , 7 , 8 };
int N = a.length;
System.out.println(findMinEqualSums(a, N));
}
}
|
Python3
import sys
def findMinEqualSums(a, N):
sum = 0
for i in range ( 0 ,N):
sum = sum + a[i]
sum1 = 0
sum2 = 0
min = sys.maxsize
for i in range ( 0 , N - 1 ):
sum1 + = a[i]
sum2 = sum - sum1
if ( abs (sum1 - sum2) < min ):
min = abs (sum1 - sum2)
if ( min = = 0 ) :
break
return min
if __name__ = = '__main__' :
a = [ 3 , 2 , 1 , 5 , 7 , 8 ]
N = len (a)
print (findMinEqualSums(a, N))
|
C#
using System;
class GFG
{
static int findMinEqualSums( int [] a, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += a[i];
}
int sum1 = 0, sum2 = 0;
int min = int .MaxValue;
for ( int i = 0; i < N; i++)
{
sum1 += a[i];
sum2 = sum - sum1;
if (Math.Abs(sum1 - sum2) < min)
{
min = Math.Abs(sum1 - sum2);
}
if (min == 0)
{
break ;
}
}
return min;
}
public static void Main()
{
int [] a = { 3, 2, 1, 5, 7, 8 };
int N = a.Length;
Console.WriteLine(findMinEqualSums(a, N));
}
}
|
PHP
<?php
function findMinEqualSums( $a , $N )
{
$sum = 0;
for ( $i = 0; $i < $N ; $i ++)
{
$sum += $a [ $i ];
}
$sum1 = 0; $sum2 = 0;
$min = PHP_INT_MAX;
for ( $i = 0; $i < $N ; $i ++)
{
$sum1 += $a [ $i ];
$sum2 = $sum - $sum1 ;
if ( abs ( $sum1 - $sum2 ) < $min )
{
$min = abs ( $sum1 - $sum2 );
}
if ( $min == 0)
{
break ;
}
}
return $min ;
}
$a = array ( 3, 2, 1, 5, 7, 8 );
$N = count ( $a );
echo (findMinEqualSums( $a , $N ));
?>
|
Javascript
<script>
function findMinEqualSums(a,N)
{
let sum = 0;
for (let i = 0; i < N; i++)
{
sum += a[i];
}
let sum1 = 0, sum2 = 0;
let min = Number.MAX_VALUE;
for (let i = 0; i < N; i++)
{
sum1 += a[i];
sum2 = sum - sum1;
if (Math.abs(sum1 - sum2) < min)
{
min = Math.abs(sum1 - sum2);
}
if (min == 0)
{
break ;
}
}
return min;
}
let a=[3, 2, 1, 5, 7, 8];
let N = a.length;
document.write(findMinEqualSums(a, N));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...