text: Nearest power of 2 of nearest perfect squares of non | Function to find nearest perfect square of num ; Calculate square root of num ; Calculate perfect square ; Find the nearest perfect square ; Function to find the power of 2 nearest to the number num ; Calculate log base 2 of num ; Highest power of 2 which is <= num ; Function to find the nearest perfect square and the nearest power of 2 of every array element whose occurrence is 1 ; Stores frequency of array elements ; Traverse the array and update frequency of current array element ; Traverse the map freq ; If the frequency is 1 ; Find nearest perfect square ; Print the nearest power of 2 ; If the any does not contain any non - repeating elements ; Driver Code
code: function perfectSquare ( num ) { let sr = Math . floor ( Math . sqrt ( num ) ) ; let a = sr * sr ; let b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( num - a ) < ( b - num ) ) { return a ; } else { return b ; } } function powerOfTwo ( num ) { let lg = Math . floor ( Math . log2 ( num ) ) ; let p = Math . pow ( 2 , lg ) ; return p ; } function uniqueElement ( arr , N ) { let ans = true ; arr . reverse ( ) ; let freq = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) } else [ freq . set ( arr [ i ] , 1 ) ] } for ( let el of freq ) { if ( el [ 1 ] == 1 ) { ans = false ; let ps = perfectSquare ( el [ 0 ] ) ; document . write ( powerOfTwo ( ps ) + ' ' ) ; } } if ( ans ) document . write ( " " ) ; } let arr = [ 4 , 11 , 4 , 3 , 4 ] ; let N = arr . length ; uniqueElement ( arr , N ) ;