Number of Even and Odd Bits

You are given a positive integer n.

Let even denote the number of even indices in the binary representation of n with value 1.

Let odd denote the number of odd indices in the binary representation of n with value 1.

Note that bits are indexed from right to left in the binary representation of a number.

Return the array [even, odd].

Example 1
Inputn = 50
Output[1,2]
The binary representation of 50 is 110010, which contains 1 on indices 1, 4, and 5, so there is 1 even index and 2 odd indices.
Example 2
Inputn = 2
Output[0,1]
The binary representation of 2 is 10, which contains 1 only on index 1, so there are 0 even indices and 1 odd index.

Constraints

  • 1 <= n <= 1000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate