Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0
for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; if ( mp . ContainsKey ( curr_sum ) ) { var v = mp [ curr_sum ] ; mp . Remove ( curr_sum ) ; mp . Add ( curr_sum , ++ v ) ; } else mp . Add ( curr_sum , 1 ) ; } int count = 0 ;