Storing frequency of each element and calculating sum simultaneously
for ( int i = 0 ; i < n ; ++ i ) { m [ A [ i ] ] ++ ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ;