Count number of ways to divide an array into two halves with same sum | Function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; Driver Code
function cntWays ( arr , n ) { if ( n == 1 ) return 0 ; var tot_sum = 0 , sum = 0 , ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) tot_sum += arr [ i ] ; for ( var i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) ans ++ ; } return ans ; } var arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; var n = arr . length ; document . write ( cntWays ( arr , n ) ) ;