📌  相关文章
📜  包含2的幂的数组,其XOR和元素之和等于X

📅  最后修改于: 2021-05-25 07:04:35             🧑  作者: Mango

给定整数X。该任务是查找并返回包含2的幂的数组,并且该数组的xor为X。
例子:

方法:答案在于数字X的二进制表示形式。
由于以2的幂表示,因此只有一个置位。如果存在两个2的不同幂,则xor将是两个数字的加法。
同样,如果要对整个数组进行xor运算,则它应等于X ,这将是该数字的二进制表示形式。
由于2的每个幂都有一个不同的置位位,因此xor和数组元素的总和将相同。
下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to return the required array
vector getArray(int n)
{
    vector 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 ans = getArray(n);
 
    // Printing the array
    for(int i : ans)
        cout << i << " ";
 
    return 0;
}


Java
// Java implementation implementation
// of the above approach
import java.util.*;
class GFG
{
 
// Function to return the required array
static Vector getArray(int n)
{
    Vector ans = new Vector();
 
    // 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 ans = getArray(n);
 
    // Printing the array
    for(Long i : ans)
        System.out.print(i + " ");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# 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


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to return the required array
static List getArray(int n)
{
    List ans = new List();
 
    // 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 ans = getArray(n);
 
    // Printing the array
    foreach(long i in ans)
        Console.Write(i + " ");
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
1 2 4 8