Array containing power of 2 whose XOR and Sum of elements equals X | Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array
function getArray ( n ) { let ans = [ ] ; let p2 = 1 ; while ( n > 0 ) { if ( n & 1 ) ans . push ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } let n = 15 ; let ans = getArray ( n ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ;