Count ways to split array into two equal sum subarrays by changing sign of any one array element | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int countSubArraySignChange ( int [ ] arr , int N ) { Dictionary < int , int > prefixCount = new Dictionary < int , int > ( ) ; Dictionary < int , int > suffixCount = new Dictionary < int , int > ( ) ; int total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] + 1 ; } else { suffixCount . Add ( arr [ i ] , 1 ) ; } } int prefixSum = 0 ; int suffixSum = 0 ; int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixCount . ContainsKey ( arr [ i ] ) ) { prefixCount [ arr [ i ] ] = prefixCount [ arr [ i ] ] + 1 ; } else { prefixCount . Add ( arr [ i ] , 1 ) ; } suffixSum = total - prefixSum ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] - 1 ; } int diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { int x = ( prefixCount . ContainsKey ( diff / 2 ) ? prefixCount : 0 ) + ( suffixCount . ContainsKey ( - diff / 2 ) ? suffixCount [ - diff / 2 ] : 0 ) ; count = count + x ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , - 3 , 3 } ; int N = arr . Length ; Console . Write ( countSubArraySignChange ( arr , N ) ) ; } }