Count number of ways to divide an array into two halves with same sum

Given an integer array of N elements. The task is to find the number of ways to split the array into two equal sum sub-arrays of non-zero lengths.

Examples:

Input : arr[] = {0, 0, 0, 0}
Output : 3
All the possible ways.
{{{0}, {0, 0, 0}}
{{0, 0}, {0, 0}}
{{0, 0, 0}, {0}}}

Input : {1, -1, 1, -1}
Output : 1

Simple Solution: A simple solution is to generate all the possible contiguous sub-arrays pairs and find there sum. If their sum is the same, we will increase the count by one.

Thus, the time complexity of the algorithm will be O(N^2) as there will be N-1 pairs in total and time complexity to find the sum of both the pairs will be O(N).

Efficient Approach: The idea is to take an auxiliary array say, aux[] to calculate the presum of the array such that for an index i aux[i] will store the sum of all elements from index 0 to index i.

By doing this we can calculate the left sum and right sum for every index of the array in constant time.

So, the idea is to:

  1. Find the sum of all the numbers of the array and store it in a variable say, S. If the sum is odd, then the answer will be 0.
  2. Traverse the array and keep calculating the sum of elements. At ith step, we will use the variable S to maintain sum of all the elements from index 0 to i.
    • Calculate sum upto the ith index.
    • If this sum equals S/2, increase the count of the number of ways by 1.
  3. Do this from i=0 to i=N-2.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count the number of ways to
// divide an array into two halves
// with the same sum
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the number of ways to
// divide an array into two halves
// with same sum
int cntWays(int arr[], int n)
{
    // if length of array is 1
    // answer will be 0 as we have
    // to split it into two
    // non-empty halves
    if (n == 1)
        return 0;
  
    // variables to store total sum,
    // current sum and count
    int tot_sum = 0, sum = 0, ans = 0;
  
    // finding total sum
    for (int i = 0; i < n; i++)
        tot_sum += arr[i];
  
    // checking if sum equals total_sum/2
    for (int i = 0; i < n - 1; i++) {
        sum += arr[i];
        if (sum == tot_sum / 2)
            ans++;
    }
  
    return ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, -1, 1, -1, 1, -1 };
  
    int n = sizeof(arr) / sizeof(int);
  
    cout << cntWays(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count the number of ways to
// divide an array into two halves
// with the same sum
class GFG 
{
  
    // Function to count the number of ways to
    // divide an array into two halves
    // with same sum
    static int cntWays(int arr[], int n)
    {
        // if length of array is 1
        // answer will be 0 as we have
        // to split it into two
        // non-empty halves
        if (n == 1
        {
            return 0;
        }
  
        // variables to store total sum,
        // current sum and count
        int tot_sum = 0, sum = 0, ans = 0;
  
        // finding total sum
        for (int i = 0; i < n; i++) 
        {
            tot_sum += arr[i];
        }
  
        // checking if sum equals total_sum/2
        for (int i = 0; i < n - 1; i++) 
        {
            sum += arr[i];
            if (sum == tot_sum / 2)
            {
                ans++;
            }
        }
  
        return ans;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = {1, -1, 1, -1, 1, -1};
  
        int n = arr.length;
  
        System.out.println(cntWays(arr, n));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count the number of ways to 
# divide an array into two halves 
# with the same sum 
  
# Function to count the number of ways to 
# divide an array into two halves 
# with same sum 
def cntWays(arr, n): 
    # if length of array is 1 
    # answer will be 0 as we have 
    # to split it into two 
    # non-empty halves 
    if (n == 1): 
        return 0
  
    # variables to store total sum, 
    # current sum and count 
    tot_sum = 0; sum = 0; ans = 0
  
    # finding total sum 
    for i in range(0,n): 
        tot_sum += arr[i]; 
  
    # checking if sum equals total_sum/2 
    for i in range(0,n-1):
        sum += arr[i]; 
        if (sum == tot_sum / 2):
            ans+=1
    return ans; 
  
# Driver Code 
arr = [1, -1, 1, -1, 1, -1 ]; 
  
n = len(arr); 
  
print(cntWays(arr, n)); 
  
# This code contributed by PrinciRaj1992 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count the number of ways to
// divide an array into two halves with
// the same sum
using System; 
  
class GFG 
{
  
    // Function to count the number of ways to
    // divide an array into two halves
    // with same sum
    static int cntWays(int []arr, int n)
    {
        // if length of array is 1
        // answer will be 0 as we have
        // to split it into two
        // non-empty halves
        if (n == 1) 
        {
            return 0;
        }
  
        // variables to store total sum,
        // current sum and count
        int tot_sum = 0, sum = 0, ans = 0;
  
        // finding total sum
        for (int i = 0; i < n; i++) 
        {
            tot_sum += arr[i];
        }
  
        // checking if sum equals total_sum/2
        for (int i = 0; i < n - 1; i++) 
        {
            sum += arr[i];
            if (sum == tot_sum / 2)
            {
                ans++;
            }
        }
  
        return ans;
    }
  
    // Driver Code
    public static void Main()
    {
        int []arr = {1, -1, 1, -1, 1, -1};
  
        int n = arr.Length;
  
        Console.WriteLine(cntWays(arr, n));
    }
}
  
// This code contributed by anuj_67..

chevron_right


Output:

2

Time Complexity: O(N)



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.



Improved By : Rajput-Ji, vt_m, princiraj1992