Count ways to split array into two equal sum subarrays by changing sign of any one array element | Function to count ways of splitting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call
function countSubArraySignChange ( arr , N ) { let prefixCount = new Map ( ) ; let suffixCount = new Map ( ) ; let total = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; if ( suffixCount . has ( arr [ i ] ) ) { suffixCount . set ( arr [ i ] , suffixCount . get ( arr [ i ] ) + 1 ) ; } else { suffixCount . set ( arr [ i ] , 1 ) ; } } let prefixSum = 0 ; let suffixSum = 0 ; let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixCount . has ( arr [ i ] ) ) { prefixCount . set ( arr [ i ] , prefixCount . get ( arr [ i ] ) + 1 ) ; } else { prefixCount . set ( arr [ i ] , 1 ) ; } suffixSum = total - prefixSum ; if ( suffixCount . has ( arr [ i ] ) ) { suffixCount . set ( arr [ i ] , suffixCount . get ( arr [ i ] ) - 1 ) ; } let diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { let x = ( prefixCount . has ( diff / 2 ) ? prefixCount . get ( diff / 2 ) : 0 ) + ( suffixCount . has ( - diff / 2 ) ? suffixCount . get ( - diff / 2 ) : 0 ) ; count = count + x ; } } return count ; } let arr = [ 2 , 2 , - 3 , 3 ] ; let N = arr . length ; document . write ( countSubArraySignChange ( arr , N ) ) ;