Talentd
FleetCode

2595. Number of Even and Odd Bits

Easy73.4% acceptance1 company

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:

Input: n = 50

Output: [1,2]

Explanation:

The binary representation of 50 is 110010.

It contains 1 on indices 1, 4, and 5.

Example 2:

Input: n = 2

Output: [0,1]

Explanation:

The binary representation of 2 is 10.

It contains 1 only on index 1.

Constraints:

  • 1 <= n <= 1000

Asked at 1 company

Hints open one at a time

Hint 1
Maintain two integer variables, even and odd, to count the number of even and odd indices in the binary representation of integer n.
Hint 2
Divide n by 2 while n is positive, and if n modulo 2 is 1, add 1 to its corresponding variable.
3:29
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def evenOddBit(self, n: int) -> List[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
50
Input
2
Your code is saved on this device.Solution