Calculate the sum of the initial array and store the frequency of each element in map
for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . ContainsKey ( A [ i ] ) ) { count [ A [ i ] ] = count [ A [ i ] ] + 1 ; } else { count . Add ( A [ i ] , 1 ) ; } }