Array containing power of 2 whose XOR and Sum of elements equals X

Given an integer X. The task is to find and return the array containing of powers of 2’s and the xor of the array is X.

Examples:

Input: X = 20
Output: 16 4



Input: X = 15
Output: 1 2 4 8

Approach: The answer lies in the binary representation of the number X.

Since in the power of 2, there is only one set bit. If there are two distinct powers of 2’s present then the xor will be the addition of both the numbers.

Similarly, if xor of the whole array will be taken then it should be equal to X and that will be the binary representation of that number.

Since there is a distinct set bit in every power of 2’s, the xor and the sum of the elements of the array will be the same.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the required array
vector<long> getArray(int n)
{
    vector<long> ans;
  
    // Store the power of 2
    long p2 = 1;
  
    // while n is greater than 0
    while (n > 0) {
          
        // if there is 1 in binary
        // representation
        if (n & 1)
            ans.push_back(p2);
  
        // Divide n by 2
        // Multiply p2 by 2
        n >>= 1;
        p2 *= 2;
    }
  
    return ans;
}
  
// Driver code
int main()
{
    long n = 15;
  
    // Get the answer
    vector<long> ans = getArray(n);
  
    // Printing the array
    for(int i : ans)
        cout << i << " ";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation implementation
// of the above approach
import java.util.*;
class GFG 
{
  
// Function to return the required array
static Vector<Long> getArray(int n)
{
    Vector<Long> ans = new Vector<Long>();
  
    // Store the power of 2
    long p2 = 1;
  
    // while n is greater than 0
    while (n > 0)
    {
          
        // if there is 1 in binary
        // representation
        if (n % 2 == 1)
            ans.add(p2);
  
        // Divide n by 2
        // Multiply p2 by 2
        n >>= 1;
        p2 *= 2;
    }
    return ans;
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 15;
  
    // Get the answer
    Vector<Long> ans = getArray(n);
  
    // Printing the array
    for(Long i : ans)
        System.out.print(i + " ");
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach 
  
# Function to return the required array 
def getArray(n) : 
  
    ans = []; 
  
    # Store the power of 2 
    p2 = 1
  
    # while n is greater than 0 
    while (n > 0) :
          
        # if there is 1 in binary 
        # representation 
        if (n & 1) :
            ans.append(p2); 
  
        # Divide n by 2 
        # Multiply p2 by 2 
        n >>= 1
        p2 *= 2
  
    return ans; 
  
# Driver code 
if __name__ == "__main__"
  
    n = 15
  
    # Get the answer 
    ans = getArray(n); 
  
    # Printing the array 
    for i in ans :
        print(i, end = " "); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function to return the required array
static List<long> getArray(int n)
{
    List<long> ans = new List<long>();
  
    // Store the power of 2
    long p2 = 1;
  
    // while n is greater than 0
    while (n > 0)
    {
          
        // if there is 1 in binary
        // representation
        if (n % 2 == 1)
            ans.Add(p2);
  
        // Divide n by 2
        // Multiply p2 by 2
        n >>= 1;
        p2 *= 2;
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args) 
{
    int n = 15;
  
    // Get the answer
    List<long> ans = getArray(n);
  
    // Printing the array
    foreach(long i in ans)
        Console.Write(i + " ");
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

1 2 4 8 


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.