Minimum positive integer required to split the array equally

Given an array of N positive integers, the task is to find the smallest positive integer 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 can be inserted is 4 between elements 5 and 7 
as part of the first subarray so that the sum of the two subarrays becomes 
equal i.e, 3 + 2 + 1 + 5 + 4 = 15 and 7 + 8 = 15.

Input : arr = { 3, 2, 2, 3 }
Output : No Extra Element required
Explanation
Equal sum of 5 is obtained by adding the first two elements and last two elements 
as separate subarrays without inserting any extra number.

Approach: Let the sum of the whole array by S. The idea is to find the left sum till index i(including it). Let this sum be L. Now the sum of the subarray arri + 1 ……. N is S – L. Let this sum be R. Since the sum of the two subarrays is supposed to be equal, the larger of the two above obtained sums L and R, should be reduced to the value of the smaller sum among these two and the difference between the larger sum and the smaller sum, will be the value of the positive integer required, which needs to be minimised.



There will be two conditions while traversing:

  1. L > R: the value of elements required to make the sum of the left and right subarrays equal will be L – R and if this value is less than the value of the minimum element calculated previously, then this becomes the minimum element required. Obviously, this element would be a part of the subarray having a lesser sum, i.e the right subarray is this case
  2. R > L: the value of the elements required to make the sum of the left and right subarrays equal will be R – L and if this value is less than the value of the minimum element calculated previously, then this becomes the minimum element required. Obviously, this element would be a part of the subarray having a lesser sum, i.e the left subarray is this case

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the minimum non-negative
// element required to split the array
// into two subarrays with equal sum
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum positive integer
// required to split array into two subarrays with equal sums
int findMinimumSplit(int arr[], int n)
{
  
    // Find the sum of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++) {
        totalSum += arr[i];
    }
  
    // leftSubarraySum stores the sum of arr[0....i] and
    // rightSubarraySum stores the sum of arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = INT_MAX;
  
    for (int i = 0; i < n - 1; i++) {
        // Find the left subarray sum and
        // corresponding right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum - leftSubarraySum;
  
        // if left subarray has larger sum, find the
        // element to be included in the right subarray
        // to make their sums equal
        if (leftSubarraySum > rightSubarraySum) {
            int element = leftSubarraySum - rightSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
        // the Right subarray has larger sum,
        // find the element to be included in
        // the left subarray to make their sums equal
        else {
            int element = rightSubarraySum - leftSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
    }
  
    return minimumElement;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 3, 2, 1, 5, 7, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    int minimumElement = findMinimumSplit(arr, n);
  
    // If 0 then no insertion is required
    if (minimumElement == 0) {
        cout << "No Extra Element Required" << endl;
    }
    else {
        cout << minimumElement << endl;
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the minimum non-negative
// element required to split the array
// into two subarrays with equal sum
import java.util.*;
  
class solution
{
  
// Function to return the minimum positive integer
// required to split array into two subarrays with equal sums
static int findMinimumSplit(int arr[], int n)
{
  
    // Find the sum of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++) {
        totalSum += arr[i];
    }
  
    // leftSubarraySum stores the sum of arr[0....i] and
    // rightSubarraySum stores the sum of arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = Integer.MAX_VALUE;
  
    for (int i = 0; i < n - 1; i++) {
        // Find the left subarray sum and
        // corresponding right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum - leftSubarraySum;
  
        // if left subarray has larger sum, find the
        // element to be included in the right subarray
        // to make their sums equal
        if (leftSubarraySum > rightSubarraySum) {
            int element = leftSubarraySum - rightSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
        // the Right subarray has larger sum,
        // find the element to be included in
        // the left subarray to make their sums equal
        else {
            int element = rightSubarraySum - leftSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
    }
  
    return minimumElement;
}
  
// Driver Code
public static void main(String args[])
{
  
    int arr[] = { 3, 2, 1, 5, 7, 8 };
    int n = arr.length;
  
    int minimumElement = findMinimumSplit(arr, n);
  
    // If 0 then no insertion is required
    if (minimumElement == 0) {
        System.out.println("No Extra Element Required");
    }
    else {
        System.out.println(minimumElement);
    }
  
}
  
}
// This code is contributed by
// Sanjit_Prasad

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the minimum 
# non-negative element required to split 
# the array into two subarrays with equal sum
import sys
  
# Function to return the minimum positive 
# integer required to split array into two
# subarrays with equal sums
def findMinimumSplit(arr, n):
  
    # Find the sum of whole array
    totalSum = 0
    for i in range(n):
        totalSum += arr[i]
  
    # leftSubarraySum stores the sum of 
    # arr[0....i] and rightSubarraySum 
    # stores the sum of arr[i + 1....n]
    leftSubarraySum = 0
    rightSubarraySum = 0
    minimumElement = sys.maxsize
  
    for i in range(n - 1):
          
        # Find the left subarray sum and
        # corresponding right subarray sum
        leftSubarraySum += arr[i]
        rightSubarraySum = totalSum - leftSubarraySum
  
        # if left subarray has larger sum, find the
        # element to be included in the right 
        # subarray to make their sums equal
        if (leftSubarraySum > rightSubarraySum):
            element = leftSubarraySum - rightSubarraySum
            if (element < minimumElement) :
                minimumElement = element
      
        # the Right subarray has larger sum,
        # find the element to be included in
        # the left subarray to make their sums equal
        else :
            element = rightSubarraySum - leftSubarraySum
            if (element < minimumElement) :
                minimumElement = element
  
    return minimumElement
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 3, 2, 1, 5, 7, 8 ]
    n = len(arr)
  
    minimumElement = findMinimumSplit(arr, n)
  
    # If 0 then no insertion is required
    if (minimumElement == 0):
        print( "No Extra Element Required" )
      
    else :
        print(minimumElement)
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the 
// minimum non-negative 
// element required to split 
// the array into two 
// subarrays with equal sum
using System;
  
class GFG
{
  
// Function to return the
// minimum positive integer
// required to split array 
// into two subarrays with
// equal sums
static int findMinimumSplit(int []arr,
                            int n)
{
  
    // Find the sum 
    // of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++) 
    {
        totalSum += arr[i];
    }
  
    // leftSubarraySum stores 
    // the sum of arr[0....i] 
    // and rightSubarraySum 
    // stores the sum of 
    // arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = int.MaxValue;
  
    for (int i = 0; i < n - 1; i++)
    {
        // Find the left subarray 
        // sum and corresponding 
        // right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum - 
                           leftSubarraySum;
  
        // if left subarray has 
        // larger sum, find the
        // element to be included 
        // in the right subarray
        // to make their sums equal
        if (leftSubarraySum > 
            rightSubarraySum) 
        {
            int element = leftSubarraySum - 
                          rightSubarraySum;
            if (element < minimumElement)
            {
                minimumElement = element;
            }
        }
          
        // the Right subarray has 
        // larger sum, find the 
        // element to be included 
        // in the left subarray to
        // make their sums equal
        else 
        {
            int element = rightSubarraySum -
                          leftSubarraySum;
            if (element < minimumElement)
            {
                minimumElement = element;
            }
        }
    }
  
    return minimumElement;
}
  
// Driver Code
public static void Main () 
{
    int []arr = {3, 2, 1, 5, 7, 8};
    int n = arr.Length;
  
    int minimumElement = 
        findMinimumSplit(arr, n);
  
    // If 0 then no 
    // insertion is required
    if (minimumElement == 0)
    {
        Console.WriteLine("No Extra "
                   "Element Required");
    }
    else 
    {
        Console.WriteLine(minimumElement);
    }
}
}
  
// This code is contributed
// by anuj_67.

chevron_right


Output:

4

Time Complexity: O(N) where N is the number of elements in the array.



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.