Number of times an array can be partitioned repetitively into two subarrays with equal sum | Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Given array ; Function call
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 ) ;