Open In App

Count the number of ways to divide an array into three contiguous parts having equal sum

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of n numbers. Our task is to find out the number of ways to divide the array into three contiguous parts such that the sum of three parts is equal. In other words, we need to find the number of index pairs i and j such that sum of elements from 0 to i-1 is equal to the sum of elements from i to j is equal to the sum of elements from j+1 to n-1.

Examples: 

Input  : arr[] = {1, 2, 3, 0, 3}
Output : 2
Following are two possible ways to partition
1) Three parts are (1, 2), (3) and (0, 3)
2) Three parts are (1, 2), (3,0) and (3)
Input : arr[] = {0, 1, -1, 0}
Output : 1
It is only one way to partition.
1) Three parts are (0), (1,-1) and (0)

Naive Approach

The idea is to find every possible index pair i and j such that array can be divided into three parts. Then find the sum of each part and if the sum of all three part are equal then increment the count

Steps to implement-

  • Initialize a variable count with a value of 0.
  • Now run two nested for loops to choose index i and j such that we will get three parts of array
  • After that initialize three variables with a value of 0 to find the sum of those three part
  • Run a for loop from 0 to i-1 to find the sum of the first part
  • Run a for loop from i to j to find the sum of the second part
  • Run a for loop from j+1 to n-1 to find the sum of the third part
  • When the sum of all three parts becomes equal then increment the count
  • In last return or print the count variable 

Code-

C++




// C++ program to count number of ways we can
// partition an array in three parts with equal
// sum.
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate the number of ways to
// divide the array into three contiguous parts
int CountContiguousParts(int arr[], int n)
{
    int count = 0; // initializing answer
 
    //Finding i,j for that
    for(int i=1;i<n-1;i++){
        for(int j=i;j<n-1;j++){
            //Initializing sum of all three parts to zero
            int sum1=0;
            int sum2=0;
            int sum3=0;
             
            //Finding sum of first part
            for(int k=0;k<i;k++){
                sum1+=arr[k];
            }
             
            //Finding sum of second part
            for(int k=i;k<=j;k++){
                sum2+=arr[k];
            }
             
            //Finding sum of third part
            for(int k=j+1;k<n;k++){
                sum3+=arr[k];
            }
             
             
            //When all part has equal sum
            if(sum1==sum2 && sum2==sum3 && sum1==sum3){
                count++;
            }
        }
    }
 
    return count;
}
 
// driver function
int main()
{
    int arr[] = { 1, 2, 3, 0, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << CountContiguousParts(arr, n);
    return 0;
}


Java




//GFG
// Java code for this approach
import java.util.*;
 
public class Main {
    // function to calculate the number of ways to divide the array
   // into three contiguous parts
    public static int countContiguousParts(int[] arr, int n) {
        int count = 0; // initializing answer
 
        //Finding i,j for that
        for(int i=1;i<n-1;i++){
            for(int j=i;j<n-1;j++){
                //Initializing sum of all three parts to zero
                int sum1=0;
                int sum2=0;
                int sum3=0;
                 
                //Finding sum of first part
                for(int k=0;k<i;k++){
                    sum1+=arr[k];
                }
                 
                //Finding sum of second part
                for(int k=i;k<=j;k++){
                    sum2+=arr[k];
                }
                 
                //Finding sum of third part
                for(int k=j+1;k<n;k++){
                    sum3+=arr[k];
                }
                 
                 
                //When all part has equal sum
                if(sum1==sum2 && sum2==sum3 && sum1==sum3){
                    count++;
                }
            }
        }
 
        return count;
    }
 
    // driver function
    public static void main(String[] args) {
        int arr[] = { 1, 2, 3, 0, 3 };
        int n = arr.length;
        System.out.println(countContiguousParts(arr, n));
    }
}
//This code is written by Sundaram


Python




# Function to calculate the number of ways to
# divide the array into three contiguous parts
def count_contiguous_parts(arr):
    count = 0  # Initializing answer
 
    n = len(arr)
 
    # Finding i, j for that
    for i in range(1, n - 1):
        for j in range(i, n - 1):
            # Initializing sum of all three parts to zero
            sum1, sum2, sum3 = 0, 0, 0
 
            # Finding sum of the first part
            for k in range(0, i):
                sum1 += arr[k]
 
            # Finding sum of the second part
            for k in range(i, j + 1):
                sum2 += arr[k]
 
            # Finding sum of the third part
            for k in range(j + 1, n):
                sum3 += arr[k]
 
            # When all parts have equal sum
            if sum1 == sum2 and sum2 == sum3 and sum1 == sum3:
                count += 1
 
    return count
 
# Driver function
if __name__ == "__main__":
    arr = [1, 2, 3, 0, 3]
    print(count_contiguous_parts(arr))


C#




using System;
 
class Program
{
    // Function to calculate the number of ways to divide the array into three contiguous parts
    static int CountContiguousParts(int[] arr, int n)
    {
        int count = 0; // Initializing the answer
 
        // Finding i, j for that
        for (int i = 1; i < n - 1; i++)
        {
            for (int j = i; j < n - 1; j++)
            {
                // Initializing sum of all three parts to zero
                int sum1 = 0;
                int sum2 = 0;
                int sum3 = 0;
 
                // Finding sum of the first part
                for (int k = 0; k < i; k++)
                {
                    sum1 += arr[k];
                }
 
                // Finding sum of the second part
                for (int k = i; k <= j; k++)
                {
                    sum2 += arr[k];
                }
 
                // Finding sum of the third part
                for (int k = j + 1; k < n; k++)
                {
                    sum3 += arr[k];
                }
 
                // When all parts have equal sum
                if (sum1 == sum2 && sum2 == sum3 && sum1 == sum3)
                {
                    count++;
                }
            }
        }
 
        return count;
    }
 
    // Driver function
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 0, 3 };
        int n = arr.Length;
        Console.WriteLine(CountContiguousParts(arr, n));
    }
}


Javascript




function CountContiguousParts(arr) {
    let count = 0; // initializing answer
 
    // Finding i, j for that
    for (let i = 1; i < arr.length - 1; i++) {
        for (let j = i; j < arr.length - 1; j++) {
            // Initializing sum of all three parts to zero
            let sum1 = 0;
            let sum2 = 0;
            let sum3 = 0;
 
            // Finding sum of the first part
            for (let k = 0; k < i; k++) {
                sum1 += arr[k];
            }
 
            // Finding sum of the second part
            for (let k = i; k <= j; k++) {
                sum2 += arr[k];
            }
 
            // Finding sum of the third part
            for (let k = j + 1; k < arr.length; k++) {
                sum3 += arr[k];
            }
 
            // When all parts have equal sum
            if (sum1 === sum2 && sum2 === sum3 && sum1 === sum3) {
                count++;
            }
        }
    }
 
    return count;
}
 
// Driver function
const arr = [1, 2, 3, 0, 3];
console.log(CountContiguousParts(arr));


Output

2





Time Complexity: O(N3), because of two nested loops to find i and j, then loops to find the sum of all three parts
Auxiliary Space: O(1), because no extra space has been used

Another Approach

If the sum of all the elements of the array is not divisible by 3 return 0. Else it is obvious that the sum of each part of each contiguous part will be equal to the sum of all elements divided by 3.

  • Step 1 : Create an array of the same size as a given array whose i-th index holds the value of the sum of elements from indices 0 to i of the given array. Let’s call it prefix array
  • Step 2 : Create another array of the same size as a given array whose i-th index the value of sum of elements from indices i to n-1 of the given array. Let’s call it suffix array.
  • Step 3 : The idea is simple, we start traversing the prefix array and suppose at the i-th index of the prefix array the value of prefix array is equal to (sum of all elements of given array)/3.
  • Step 4 : For i found in the above step we look into the suffix array from (i+2)-th index and wherever the value of suffix array is equal to (sum of all elements of given array)/3, we increase the counter variable by 1.

To implement step 4 we traverse suffix array and wherever the value of suffix array is equal to the sum of all elements of given array/3 we push that index of suffix array into the vector. And we do a binary search in the vector to calculate the number of values in suffix array which are as according to step 4.

We search in suffix array because there should be at least 1 element between the first and third part.

For more explanation see implementation below:

Implementation:

C++




// C++ program to count number of ways we can
// partition an array in three parts with equal
// sum.
#include <bits/stdc++.h>
using namespace std;
 
// binary search to find the number of required
// indices in suffix array. Returns index of
// first element which is greater than x.
int binarysearch(vector<int>& v, int x)
{
    int low = 0, high = v.size() - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (v[mid] <= x)
            low = mid + 1;
        else if (v[mid] > x && v[mid - 1] <= x)
            return mid;
        else if (v[mid] > x && mid == 0)
            return mid;
        else
            high = mid - 1;
    }
    return -1;
}
 
// function to calculate the number of ways to
// divide the array into three contiguous parts
int CountContiguousParts(int arr[], int n)
{
    int count = 0; // initializing answer
 
    // Creating and filling  prefix array
    int prefix[n];
    prefix[0] = arr[0];
    for (int i = 1; i < n; i++)
        prefix[i] = prefix[i - 1] + arr[i];
 
    // Total sum of elements is equal to last
    // value in prefix array.
    int total_sum = prefix[n - 1];
 
    // If sum of all the elements is not divisible
    // by 3, we can't divide array in 3 parts of
    // same sum.
    if (total_sum % 3 != 0)
        return 0;
 
    // Creating and filling suffix array
    int suffix[n];
    suffix[n - 1] = arr[n - 1];
    for (int i = n - 2; i >= 0; i--)
        suffix[i] = suffix[i + 1] + arr[i];
 
    // Storing all indexes with suffix
    // sum equal to total sum by 3.
    vector<int> v;
    for (int i = 0; i < n; i++)
        if (suffix[i] == total_sum / 3)
            v.push_back(i);
 
    // Traversing the prefix array and
    // doing binary search in above vector
    for (int i = 0; i < n; i++) {
        // We found a prefix with total_sum/3
        if (prefix[i] == total_sum / 3) {
            // Find first index in v[] which
            // is greater than i+1.
            int res = binarysearch(v, i + 1);
 
            if (res != -1)
                count += v.size() - res;
        }
    }
 
    return count;
}
 
// driver function
int main()
{
    int arr[] = { 1, 2, 3, 0, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << CountContiguousParts(arr, n);
    return 0;
}


Java




// Java program to count number of ways we can
// partition an array in three parts with equal
// sum.
import java.util.*;
 
class GFG {
 
    // binary search to find the number of required
    // indices in suffix array. Returns index of
    // first element which is greater than x.
    static int binarysearch(Vector<Integer> v, int x)
    {
        int low = 0, high = v.size() - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (v.get(mid) <= x)
                low = mid + 1;
            else if (v.get(mid) > x && v.get(mid) <= x)
                return mid;
            else if (v.get(mid) > x && mid == 0)
                return mid;
            else
                high = mid - 1;
        }
        return -1;
    }
 
    // function to calculate the number of ways to
    // divide the array into three contiguous parts
    static int CountContiguousParts(int arr[], int n)
    {
        int count = 0; // initializing answer
 
        // Creating and filling prefix array
        int[] prefix = new int[n];
        prefix[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix[i] = prefix[i - 1] + arr[i];
 
        // Total sum of elements is equal to last
        // value in prefix array.
        int total_sum = prefix[n - 1];
 
        // If sum of all the elements is not divisible
        // by 3, we can't divide array in 3 parts of
        // same sum.
        if (total_sum % 3 != 0)
            return 0;
 
        // Creating and filling suffix array
        int[] suffix = new int[n];
        suffix[n - 1] = arr[n - 1];
        for (int i = n - 2; i >= 0; i--)
            suffix[i] = suffix[i + 1] + arr[i];
 
        // Storing all indexes with suffix
        // sum equal to total sum by 3.
        Vector<Integer> v = new Vector<>();
        for (int i = 0; i < n; i++)
            if (suffix[i] == total_sum / 3)
                v.add(i);
 
        // Traversing the prefix array and
        // doing binary search in above vector
        for (int i = 0; i < n; i++) {
            // We found a prefix with total_sum/3
            if (prefix[i] == total_sum / 3) {
                // Find first index in v[] which
                // is greater than i+1.
                int res = binarysearch(v, i + 1);
 
                if (res != -1)
                    count += v.size() - res;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 0, 3 };
        int n = arr.length;
        System.out.println(CountContiguousParts(arr, n));
    }
}
 
// This code is contributed by Princi Singh


Python3




# Python 3 program to count the number of ways we can
# partition an array in three parts with equal
# sum.
 
# binary search to find the number of required
# indices in suffix array. Returns index of
# first element which is greater than x.
 
 
def binarysearch(v, x):
    low = 0
    high = len(v) - 1
    while (low <= high):
        mid = int((low + high) / 2)
        if (v[mid] <= x):
            low = mid + 1
        elif (v[mid] > x and v[mid - 1] <= x):
            return mid
        elif (v[mid] > x and mid == 0):
            return mid
        else:
            high = mid - 1
 
    return -1
 
# function to calculate the number of ways to
# divide the array into three contiguous parts
 
 
def CountContiguousParts(arr, n):
    count = 0  # initializing answer
 
    # Creating and filling prefix array
    prefix = [0 for i in range(n)]
    prefix[0] = arr[0]
    for i in range(1, n, 1):
        prefix[i] = prefix[i - 1] + arr[i]
 
    # Total sum of elements is equal to last
    # value in prefix array.
    total_sum = prefix[n - 1]
 
    # If sum of all the elements is not divisible
    # by 3, we can't divide array in 3 parts of
    # same sum.
    if (total_sum % 3 != 0):
        return 0
 
    # Creating and filling suffix array
    suffix = [0 for i in range(n)]
    suffix[n - 1] = arr[n - 1]
    i = n - 2
    while(i >= 0):
        suffix[i] = suffix[i + 1] + arr[i]
        i -= 1
 
    # Storing all indexes with suffix
    # sum equal to total sum by 3.
    v = []
    for i in range(n):
        if (suffix[i] == int(total_sum / 3)):
            v.append(i)
 
    # Traversing the prefix array and
    # doing binary search in above vector
    for i in range(n):
 
        # We found a prefix with total_sum/3
        if (prefix[i] == int(total_sum / 3)):
 
            # Find first index in v[] which
            # is greater than i+1.
            res = binarysearch(v, i + 1)
 
            if (res != -1):
                count += len(v) - res
 
    return count
 
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 0, 3]
    n = len(arr)
    print(CountContiguousParts(arr, n))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# program to count number of ways we can
// partition an array in three parts with equal
using System;
using System.Collections.Generic;
 
class GFG {
 
    // binary search to find the number of required
    // indices in suffix array. Returns index of
    // first element which is greater than x.
    static int binarysearch(List<int> v, int x)
    {
        int low = 0, high = v.Count - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (v[mid] <= x)
                low = mid + 1;
            else if (v[mid] > x && v[mid] <= x)
                return mid;
            else if (v[mid] > x && mid == 0)
                return mid;
            else
                high = mid - 1;
        }
        return -1;
    }
 
    // function to calculate the number of ways to
    // divide the array into three contiguous parts
    static int CountContiguousParts(int[] arr, int n)
    {
        int count = 0; // initializing answer
 
        // Creating and filling prefix array
        int[] prefix = new int[n];
        prefix[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix[i] = prefix[i - 1] + arr[i];
 
        // Total sum of elements is equal to last
        // value in prefix array.
        int total_sum = prefix[n - 1];
 
        // If sum of all the elements is not divisible
        // by 3, we can't divide array in 3 parts of
        // same sum.
        if (total_sum % 3 != 0)
            return 0;
 
        // Creating and filling suffix array
        int[] suffix = new int[n];
        suffix[n - 1] = arr[n - 1];
        for (int i = n - 2; i >= 0; i--)
            suffix[i] = suffix[i + 1] + arr[i];
 
        // Storing all indexes with suffix
        // sum equal to total sum by 3.
        List<int> v = new List<int>();
        for (int i = 0; i < n; i++)
            if (suffix[i] == total_sum / 3)
                v.Add(i);
 
        // Traversing the prefix array and
        // doing binary search in above vector
        for (int i = 0; i < n; i++) {
            // We found a prefix with total_sum/3
            if (prefix[i] == total_sum / 3) {
                // Find first index in v[] which
                // is greater than i+1.
                int res = binarysearch(v, i + 1);
 
                if (res != -1)
                    count += v.Count - res;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = { 1, 2, 3, 0, 3 };
        int n = arr.Length;
        Console.WriteLine(CountContiguousParts(arr, n));
    }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript program to count number
// of ways we can partition an array
// in three parts with equal sum.
     
// Binary search to find the number of required
// indices in suffix array. Returns index of
// first element which is greater than x.
function binarysearch(v, x)
{
    let low = 0, high = v.length - 1;
     
    while (low <= high)
    {
        let mid = Math.floor((low + high) / 2);
         
        if (v[mid] <= x)
            low = mid + 1;
        else if (v[mid] > x && v[mid] <= x)
            return mid;
        else if (v[mid] > x && mid == 0)
            return mid;
        else
            high = mid - 1;
    }
    return -1;
}
 
// Function to calculate the number of ways to
// divide the array into three contiguous parts
function CountContiguousParts(arr, n)
{
     
    // Initializing answer
    let count = 0;
     
    // Creating and filling prefix array
    let prefix = new Array(n);
    prefix[0] = arr[0];
     
    for(let i = 1; i < n; i++)
        prefix[i] = prefix[i - 1] + arr[i];
     
    // Total sum of elements is equal to last
    // value in prefix array.
    let total_sum = prefix[n - 1];
     
    // If sum of all the elements is not divisible
    // by 3, we can't divide array in 3 parts of
    // same sum.
    if (total_sum % 3 != 0)
        return 0;
     
    // Creating and filling suffix array
    let suffix = new Array(n);
    suffix[n - 1] = arr[n - 1];
     
    for(let i = n - 2; i >= 0; i--)
        suffix[i] = suffix[i + 1] + arr[i];
     
    // Storing all indexes with suffix
    // sum equal to total sum by 3.
    let v = [];
    for(let i = 0; i < n; i++)
        if (suffix[i] == Math.floor(total_sum / 3))
            v.push(i);
     
    // Traversing the prefix array and
    // doing binary search in above vector
    for(let i = 0; i < n; i++)
    {
         
        // We found a prefix with total_sum/3
        if (prefix[i] == Math.floor(total_sum / 3))
        {
             
            // Find first index in v[] which
            // is greater than i+1.
            let res = binarysearch(v, i + 1);
             
            if (res != -1)
                count += v.length - res;
        }
    }
    return count;
}
 
// Driver Code
let arr = [ 1 , 2 , 3 , 0 , 3 ];
let  n = arr.length;
 
document.write(CountContiguousParts(arr, n));
 
// This code is contributed by avanitrachhadiya2155
 
</script>


Output

2





Time Complexity is O(n log n)
Auxiliary Space: O(n)

Efficient Approach [ O(n) solution ] :

  1. If sum of all the elements of the array is not divisible by 3, return 0.
  2. It is obvious that the sum of each part of each contiguous part will be equal to the sum of all elements divided by 3.
  3. Let’s create an array cnt[ ], where cnt[ i ] equals 1, if the sum of elements from i-th to n-th equals Array_Sum/3 else 0. Now, calculate the cumulative sum of the cnt array from the last index.
  4. Thus, we receive very simple solution: for each prefix of initial array 1…i with the sum that equals Array_Sum/3 we need to add to the answer sums[ i+2].

Below is the code for the above approach.  

C++




// C++ program to count the ways to break the array in 3
// equal parts having equal sum.
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the no of ways
int countways(int a[], int n)
{
    int cnt[n] = { 0 }, s = 0;
 
    // Calculating the sum of the array
    // and storing it in variable s
    for (int i = 0; i < n; i++) {
        s += a[i];
    }
 
    // Checking s is divisible by 3 or not
    if (s % 3 != 0)
        return 0;
 
    // Calculating the sum of each part
    s /= 3;
 
    int ss = 0;
 
    // If the sum of elements from i-th to n-th
    // equals to sum of part putting 1 in cnt
    // array otherwise 0.
    for (int i = n - 1; i >= 0; i--) {
        ss += a[i];
        if (ss == s)
            cnt[i] = 1;
    }
 
    // Calculating the cumulative sum
    // of the array cnt from the last index.
    for (int i = n - 2; i >= 0; i--)
        cnt[i] += cnt[i + 1];
 
    int ans = 0;
    ss = 0;
 
    // Calculating answer using original
    // and cnt array.
    for (int i = 0; i + 2 < n; i++) {
        ss += a[i];
        if (ss == s)
            ans += cnt[i + 2];
    }
    return ans;
}
 
// Driver function
int main()
{
    int n = 5;
    int a[] = { 1, 2, 3, 0, 3 };
    cout << countways(a, n) << endl;
    return 0;
}
 
// This code is contributed by ajaymakvana.


C




// C++ program to count the ways
// to break the array in 3 equal parts
// having equal sum.
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the no of ways
int countways(int a[], int n)
{
    int cnt[n] = { 0 }, s = 0;
 
    // Calculating the sum of the array
    // and storing it in variable s
    for (int i = 0; i < n; i++) {
        s += a[i];
    }
 
    // Checking s is divisible by 3 or not
    if (s % 3 != 0)
        return 0;
 
    // Calculating the sum of each part
    s /= 3;
 
    int ss = 0;
 
    // If the sum of elements from i-th to n-th
    // equals to sum of part putting 1 in cnt
    // array otherwise 0.
    for (int i = n - 1; i >= 0; i--) {
        ss += a[i];
        if (ss == s)
            cnt[i] = 1;
    }
 
    // Calculating the cumulative sum
    // of the array cnt from the last index.
    for (int i = n - 2; i >= 0; i--)
        cnt[i] += cnt[i + 1];
 
    int ans = 0;
    ss = 0;
 
    // Calculating answer using original
    // and cnt array.
    for (int i = 0; i + 2 < n; i++) {
        ss += a[i];
        if (ss == s)
            ans += cnt[i + 2];
    }
    return ans;
}
 
// Driver function
int main()
{
    int n = 5;
    int a[] = { 1, 2, 3, 0, 3 };
    cout << countways(a, n) << endl;
    return 0;
}


Java




// Java program to count the ways to
// break the array in 3 equal parts
// having equal sum.
import java.io.*;
 
class GFG {
 
    // Function to count the no of ways
    static int countways(int a[], int n)
    {
        int cnt[] = new int[n];
        int s = 0;
 
        // Calculating the sum of the array
        // and storing it in variable s
        for (int i = 0; i < n; i++) {
            s += a[i];
        }
 
        // Checking s is divisible by 3 or not
        if (s % 3 != 0)
            return 0;
 
        // Calculating the sum of each part
        s /= 3;
 
        int ss = 0;
 
        // If the sum of elements from i-th to n-th
        // equals to sum of part putting 1 in cnt
        // array otherwise 0.
        for (int i = n - 1; i >= 0; i--) {
            ss += a[i];
            if (ss == s)
                cnt[i] = 1;
        }
 
        // Calculating the cumulative sum
        // of the array cnt from the last index.
        for (int i = n - 2; i >= 0; i--)
            cnt[i] += cnt[i + 1];
 
        int ans = 0;
        ss = 0;
 
        // Calculating answer using original
        // and cnt array.
        for (int i = 0; i + 2 < n; i++) {
            ss += a[i];
            if (ss == s)
                ans += cnt[i + 2];
        }
        return ans;
    }
 
    // Driver function
    public static void main(String[] args)
    {
        int n = 5;
        int a[] = { 1, 2, 3, 0, 3 };
        System.out.println(countways(a, n));
    }
}
 
// This code is contributed by anuj_67.


Python3




# Python3 program to count the ways
# to break the array in 3 equal parts
# having equal sum.
 
# Function to count the no of ways
 
 
def countways(a, n):
 
    cnt = [0 for i in range(n)]
    s = 0
 
    # Calculating the sum of the array
    # and storing it in variable s
    s = sum(a)
 
    # Checking s is divisible by 3 or not
    if (s % 3 != 0):
        return 0
 
    # Calculating the sum of each part
    s //= 3
 
    ss = 0
 
    # If the sum of elements from i-th
    # to n-th equals to sum of part
    # putting 1 in cnt array otherwise 0.
    for i in range(n - 1, -1, -1):
 
        ss += a[i]
        if (ss == s):
            cnt[i] = 1
 
    # Calculating the cumulative sum
    # of the array cnt from the last index.
    for i in range(n - 2, -1, -1):
        cnt[i] += cnt[i + 1]
 
    ans = 0
    ss = 0
 
    # Calculating answer using original
    # and cnt array.
    for i in range(0, n - 2):
        ss += a[i]
        if (ss == s):
            ans += cnt[i + 2]
 
    return ans
 
 
# Driver Code
n = 5
a = [1, 2, 3, 0, 3]
print(countways(a, n))
 
# This code is contributed
# by mohit kumar


C#




// C# program to count the ways to
// break the array in 3 equal parts
// having an equal sum.
using System;
 
class GFG {
 
    // Function to count the no of ways
    static int countways(int[] a, int n)
    {
        int[] cnt = new int[n];
        int s = 0;
 
        // Calculating the sum of the array
        // and storing it in variable s
        for (int i = 0; i < n; i++) {
            s += a[i];
        }
 
        // Checking s is divisible by 3 or not
        if (s % 3 != 0)
            return 0;
 
        // Calculating the sum of each part
        s /= 3;
 
        int ss = 0;
 
        // If the sum of elements from i-th to n-th
        // equals to sum of part putting 1 in cnt
        // array otherwise 0.
        for (int i = n - 1; i >= 0; i--) {
            ss += a[i];
            if (ss == s)
                cnt[i] = 1;
        }
 
        // Calculating the cumulative sum
        // of the array cnt from the last index.
        for (int i = n - 2; i >= 0; i--)
            cnt[i] += cnt[i + 1];
 
        int ans = 0;
        ss = 0;
 
        // Calculating answer using original
        // and cnt array.
        for (int i = 0; i + 2 < n; i++) {
            ss += a[i];
            if (ss == s)
                ans += cnt[i + 2];
        }
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        int[] a = { 1, 2, 3, 0, 3 };
        Console.Write(countways(a, n));
    }
}
 
// This code is contributed by Ita_c.


Javascript




<script>
 
// Javascript program to count the ways to
// break the array in 3 equal parts
// having equal sum.
     
    // Function to count the no of ways
    function countways(a,n)
    {
        let cnt = new Array(n);
        let s = 0;
          
        // Calculating the sum of the array
        // and storing it in variable s
        for (let i = 0 ; i < n ; i++)
        {
            s += a[i];
        }
      
        // Checking s is divisible by 3 or not
        if (s % 3 != 0)
            return 0;
          
        // Calculating the sum of each part
        s = Math.floor(s/3);
          
        let ss = 0;
          
        // If the sum of elements from i-th to n-th
        // equals to sum of part putting 1 in cnt
        // array otherwise 0.
        for (let i = n-1; i >= 0 ; i--)
        {
            ss += a[i];
            if (ss == s)
                cnt[i] = 1;
        }
          
        // Calculating the cumulative sum
        // of the array cnt from the last index.
        for (let i = n-2 ; i >= 0 ; i--)
            cnt[i] += cnt[i + 1];
          
        let ans = 0;
        ss = 0;
          
        // Calculating answer using original
        // and cnt array.
        for (let i = 0 ; i+2 < n ; i++)
        {
            ss += a[i];
            if (ss == s)
                ans += cnt[i + 2];
        }
        return ans;
    }
     
    // Driver function
    let n = 5;
    let a=[1, 2, 3, 0, 3];
    document.write(countways(a, n));
       
    // This code is contributed by rag2127
     
</script>


Output

2





Time Complexity: O(n)
Auxiliary Space: O(n)

This approach is contributed by Abhishek Sharma. This article is contributed by Ayush Jha.  



Last Updated : 13 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads