Open In App

Minimum positive integer required to split the array equally

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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


Python 3




# 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


C#




// 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.


Javascript




<script>
// Javascript program to find the minimum non-negative
// element required to split the array
// into two subarrays with equal sum
 
     
    // Function to return the minimum positive integer
    // required to split array into two subarrays with equal sums
    function findMinimumSplit(arr,n)
    {
        // Find the sum of whole array
    let totalSum = 0;
    for (let 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]
    let leftSubarraySum = 0;
    let rightSubarraySum = 0;
    let minimumElement = Number.MAX_VALUE;
   
    for (let 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) {
            let 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 {
            let element = rightSubarraySum - leftSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
    }
   
    return minimumElement;
    }
     
    // Driver Code
    let arr=[3, 2, 1, 5, 7, 8];
    let n = arr.length;
    let minimumElement = findMinimumSplit(arr, n);
    // If 0 then no insertion is required
    if (minimumElement == 0) {
        document.write("No Extra Element Required");
    }
    else {
        document.write(minimumElement);
    }
     
     
 
// This code is contributed by avanitrachhadiya2155
</script>


Output

4

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



Last Updated : 26 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads