Array containing power of 2 whose XOR and Sum of elements equals X | C # implementation of the approach ; 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
using System ; using System . Collections . Generic ; class GFG { static List < long > getArray ( int n ) { List < long > ans = new List < long > ( ) ; long p2 = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) ans . Add ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } public static void Main ( String [ ] args ) { int n = 15 ; List < long > ans = getArray ( n ) ; foreach ( long i in ans ) Console . Write ( i + " ▁ " ) ; } }