Number of times an array can be partitioned repetitively into two subarrays with equal sum
Given an array arr[] of size N, the task is to find the number of times the array can be partitioned repetitively into two subarrays such that the sum of the elements of both the subarrays is the same.
Examples:
Input: arr[] = { 2, 2, 2, 2 }
Output: 3
Explanation:
1. Make the first partition after index 1. Remaining arrays are {2, 2} on the right side and left side both.
2. Consider the left subarray {2, 2}. Make a partition after index 0 of this left subarray.
Now two similar subarrays with one element each i.e. {2} are formed which cannot be sub-divided.
3. Consider the right subarray {2, 2}. Make a partition after index 0 of this left subarray.
Now two similar subarrays with one element each i.e. {2} are formed which cannot be sub-divided.
Hence the output is 3 as the array was partitioned 3 times.
Input: arr[] = {12, 3, 3, 0, 3, 3}
Output: 4
Explanation:
1. The first partition is after index 0. Remaining array is arr[] = {3, 3, 0, 3, 3}.
2. The second partition is after index 1. The remaining array is {3, 3}, and {0, 3, 3}.
3. The third partition is after index 0 in array {3, 3}.
4. The fourth partition is after 1 in the array {0, 3, 3}
The remaining array is {0, 3}, and {3} which cannot be sub-divided.
Hence the output is 4.
Approach: The idea is to use Recursion. Below are the steps:
- Find the prefix-sum of the given array arr[] and store it in an array pref[].
- Iterate from the start position to the end position.
- For each possible partition index(say K), if prefix_sum[K] – prefix_sum[start-1] = prefix_sum[end] – prefix_sum[k] then the partition is valid.
- If a partition is valid in the above step then proceed with the left and right sub-arrays separately and determine whether these two subarrays form a valid partition or not.
- Repeat the step 3 and 4 for both the left and right partition until any further partition isn’t possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int splitArray( int start, int end,
int * arr,
int * prefix_sum)
{
if (start >= end)
return 0;
for ( int k = start; k < end; ++k) {
if ((prefix_sum[k] - prefix_sum[start - 1])
== (prefix_sum[end] - prefix_sum[k])) {
return 1 + splitArray(start,
k,
arr,
prefix_sum)
+ splitArray(k + 1,
end,
arr,
prefix_sum);
}
}
return 0;
}
void solve( int arr[], int n)
{
int prefix_sum[n + 1];
prefix_sum[0] = 0;
for ( int i = 1; i <= n; ++i) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i - 1];
}
cout << splitArray(1, n,
arr,
prefix_sum);
}
int main()
{
int arr[] = { 12, 3, 3, 0, 3, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
solve(arr, N);
return 0;
}
|
Java
class GFG{
static int splitArray( int start, int end,
int [] arr,
int [] prefix_sum)
{
if (start >= end)
return 0 ;
for ( int k = start; k < end; ++k)
{
if ((prefix_sum[k] - prefix_sum[start - 1 ]) ==
(prefix_sum[end] - prefix_sum[k]))
{
return 1 + splitArray(start, k, arr, prefix_sum) +
splitArray(k + 1 , end, arr, prefix_sum);
}
}
return 0 ;
}
static void solve( int arr[], int n)
{
int []prefix_sum = new int [n + 1 ];
prefix_sum[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; ++i)
{
prefix_sum[i] = prefix_sum[i - 1 ] +
arr[i - 1 ];
}
System.out.print(splitArray( 1 , n, arr,
prefix_sum));
}
public static void main(String[] args)
{
int arr[] = { 12 , 3 , 3 , 0 , 3 , 3 };
int N = arr.length;
solve(arr, N);
}
}
|
Python3
def splitArray(start, end, arr, prefix_sum):
if (start > = end):
return 0
for k in range (start, end):
if ((prefix_sum[k] - prefix_sum[start - 1 ]) = =
(prefix_sum[end] - prefix_sum[k])) :
return ( 1 + splitArray(start, k, arr,
prefix_sum) +
splitArray(k + 1 , end, arr,
prefix_sum))
return 0
def solve(arr, n):
prefix_sum = [ 0 ] * (n + 1 )
prefix_sum[ 0 ] = 0
for i in range ( 1 , n + 1 ):
prefix_sum[i] = (prefix_sum[i - 1 ] +
arr[i - 1 ])
print (splitArray( 1 , n, arr, prefix_sum))
arr = [ 12 , 3 , 3 , 0 , 3 , 3 ]
N = len (arr)
solve(arr, N)
|
C#
using System;
class GFG{
static int splitArray( int start, int end,
int [] arr,
int [] prefix_sum)
{
if (start >= end)
return 0;
for ( int k = start; k < end; ++k)
{
if ((prefix_sum[k] -
prefix_sum[start - 1]) ==
(prefix_sum[end] -
prefix_sum[k]))
{
return 1 + splitArray(start, k, arr,
prefix_sum) +
splitArray(k + 1, end, arr,
prefix_sum);
}
}
return 0;
}
static void solve( int []arr, int n)
{
int []prefix_sum = new int [n + 1];
prefix_sum[0] = 0;
for ( int i = 1; i <= n; ++i)
{
prefix_sum[i] = prefix_sum[i - 1] +
arr[i - 1];
}
Console.Write(splitArray(1, n, arr,
prefix_sum));
}
public static void Main(String[] args)
{
int []arr = { 12, 3, 3, 0, 3, 3 };
int N = arr.Length;
solve(arr, N);
}
}
|
Javascript
<script>
function splitArray(start, end, arr, prefix_sum)
{
if (start >= end)
return 0;
for (let k = start; k < end; ++k)
{
if ((prefix_sum[k] - prefix_sum[start - 1]) ==
(prefix_sum[end] - prefix_sum[k]))
{
return 1 + splitArray(start, k, arr, prefix_sum) +
splitArray(k + 1, end, arr, prefix_sum);
}
}
return 0;
}
function solve(arr, n)
{
let prefix_sum = Array.from({length: n+1}, (_, i) => 0);
prefix_sum[0] = 0;
for (let i = 1; i <= n; ++i)
{
prefix_sum[i] = prefix_sum[i - 1] +
arr[i - 1];
}
document.write(splitArray(1, n, arr,
prefix_sum));
}
let arr = [ 12, 3, 3, 0, 3, 3 ];
let N = arr.length;
solve(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
04 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...