Count of elements which cannot form any pair whose sum is power of 2 | Function to calculate and return the count of elements ; Stores the frequencies of every array element ; Stores the count of removals ; For every element , check if it can form a sum equal to any power of 2 with any other element ; Store Math . pow ( 2 , j ) - a [ i ] ; Check if s is present in the array ; If frequency of s exceeds 1 ; If s has frequency 1 but is different from a [ i ] ; Pair possible ; If no pair possible for the current element ; Return the answer ; Driver code
function powerOfTwo ( a , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( a [ i ] ) ) { mp . set ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . set ( a [ i ] , 1 ) ; } } let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let f = false ; for ( let j = 0 ; j < 31 ; j ++ ) { let s = ( 1 << j ) - a [ i ] ; if ( mp . has ( s ) && ( mp . get ( s ) > 1 || mp . get ( s ) == 1 && s != a [ i ] ) ) f = true ; } if ( f == false ) count ++ ; } return count ; } let a = [ 6 , 2 , 11 ] ; let n = a . length ; document . write ( powerOfTwo ( a , n ) ) ;