Problem · Bit Manipulation
Powers of 2
Given an array of integers, determine whether each is a power of 2, where powers of 2 are [1, 2, 4, 8, 16, 32, 64...]. For each integer evaluated, append to an array a value of 1 if the number is a power of 2 or 0 otherwise.
Complete the function isPower in the editor below.
isPower has the following parameter(s):
int arr[n]: an array of integers
Returns
int[n]: array of binary integers where each index i contains a 1 if arr[i] is a power of 2 or a 0 if it is not
Examples
01 · Example 1
arr = [1, 3, 8, 12, 16] return = [1, 0, 1, 0, 1]
1 = 2^0, 8 = 2^3 and 16 = 2^4. The return array is [1, 0, 1, 0, 1].
Constraints
1 ≤ n ≤ 1000 ≤ arr[i] ≤ 5 x 10^7
More Oracle problems
public int[] isPower(int[] arr) {
// write your code here
}
arr[1, 3, 8, 12, 16]
expected[1, 0, 1, 0, 1]
sign in to submit