Leetcode Problem 2595. Number of Even and Odd Bits

2595. Number of Even and Odd Bits

Leetcode Solutions

Bit Manipulation and Counting

  1. Initialize two counters even and odd to 0.
  2. Initialize an index i to 0.
  3. While n is not 0: a. Check if the least significant bit of n is 1 using n & 1. b. If i is even and the bit is 1, increment even. c. If i is odd and the bit is 1, increment odd. d. Right shift n by 1 to move to the next bit. e. Increment the index i.
  4. Return the array [even, odd].
UML Thumbnail

String Conversion and Iteration

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...