2659. Number of Even and Odd Bits
My accepted C++ solution to LeetCode problem 2659, Number of Even and Odd Bits, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 9.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 9.3MB, accepted 2025-12-27.
class Solution {
public:
vector<int> evenOddBit(int n) {
int even = 0, odd = 0, i = 0;
while (n) {
if (n & 1) {
if (i % 2 == 0) even++;
else odd++;
}
n >>= 1;
i++;
}
return {even, odd};
}
};