FastPrepFastPrep
Problem Brief

Powers of 2

FULLTIMEOA

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.

Function Description

Complete the function isPower in the editor below.

isPower has the following parameter(s):

  1. 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

1Example 1

Input
arr = [1, 3, 8, 12, 16]
Output
[1, 0, 1, 0, 1]
Explanation

1 = 2^0, 8 = 2^3 and 16 = 2^4. The return array is [1, 0, 1, 0, 1].

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 100
  • 0 ≤ arr[i] ≤ 5 x 10^7
public int[] isPower(int[] arr) {
    // write your code here
}
Input

arr

[1, 3, 8, 12, 16]

Output

[1, 0, 1, 0, 1]

Sign in to submit your solution.