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