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.
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:
1 2 4 8
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Elements of Array which can be expressed as power of prime numbers
- Elements of Array which can be expressed as power of some integer to given exponent K
- Smallest power of 2 which is greater than or equal to sum of array elements
- Check if any permutation of N equals any power of K
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Count of elements which cannot form any pair whose sum is power of 2
- Count of elements which are power of 2 in a given range subarray for Q queries
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count of even and odd power pairs in an Array
- Count pairs in array such that one element is power of another
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Minimum element whose n-th power is greater than product of an array of size n
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Find the sum of power of bit count raised to the power B
- Check if ceil of number divided by power of two exist in sorted array
- Find power of power under mod of a prime
- Compute power of power k times % m
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.