Count ways to split array into two equal sum subarrays by changing sign of any one array element | Function to count ways of spliting 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
def countSubArraySignChange ( arr , N ) : NEW_LINE INDENT prefixCount = { } NEW_LINE suffixCount = { } NEW_LINE total = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE suffixCount [ arr [ i ] ] = suffixCount . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT prefixSum = 0 NEW_LINE suffixSum = 0 NEW_LINE count = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT prefixSum += arr [ i ] NEW_LINE prefixCount [ arr [ i ] ] = prefixCount . get ( arr [ i ] , 0 ) + 1 NEW_LINE suffixSum = total - prefixSum NEW_LINE suffixCount [ arr [ i ] ] -= 1 NEW_LINE diff = prefixSum - suffixSum NEW_LINE if ( diff % 2 == 0 ) : NEW_LINE INDENT y , z = 0 , 0 NEW_LINE if - diff // 2 in suffixCount : NEW_LINE INDENT y = suffixCount [ - dff // 2 ] NEW_LINE DEDENT if diff // 2 in prefixCount : NEW_LINE INDENT z = prefixCount NEW_LINE DEDENT x = z + y NEW_LINE count = count + x NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , - 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countSubArraySignChange ( arr , N ) ) NEW_LINE DEDENT