4. Top C++ Solution | 100% Faster | 0ms Runtime | Beginner Friendly Optimized !!
IntuitionThe problem involves analyzing the binary representation of an integer n and determining how many 1 bits appear at even and odd positions. The key intu
### **Intuition**

The problem involves analyzing the binary representation of an integer `n` and determining how many `1` bits appear at even and odd positions. The key intuition here is:
1. In the binary representation of a number, the rightmost bit is the **0th bit (even)**, the next bit is the **1st bit (odd)**, and so on.
2. By checking each bit one by one (using bitwise operations), we can determine if the bit is `1` and whether its position is even or odd. We increment the corresponding count accordingly.

This can be efficiently done by iteratively examining the least significant bit and right-shifting the number.

---

### **Approach**

1. **Initialization:**
   - Create a vector `ans = {0, 0}` where:
     - `ans[0]` will store the count of `1` bits at even positions.
     - `ans[1]` will store the count of `1` bits at odd positions.
   - Initialize an `index` variable to `0` to track the current bit's position.

2. **Processing Each Bit:**
   - Use a `while` loop that runs as long as `n != 0`.
   - Extract the least significant bit using `bit = n & 1`.
     - If `bit == 1`:
       - If the current `index` is even (`index % 2 == 0`), increment `ans[0]`.
       - Otherwise, increment `ans[1]`.
     - If `bit == 0`, skip to the next step.
   - Right-shift the number (`n = n >> 1`) to process the next bit.
   - Increment the `index` to track the position of the next bit.

3. **Return the Result:**
   - After processing all bits, return the `ans` vector containing the counts of `1` bits at even and odd positions.
---
# Complexity
- Time complexity: O(log N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
// Please Upvote my Solution
class Solution {
public:
    vector<int> evenOddBit(int n) {

        vector<int>ans;
        ans = {0,0};

        int index = 0;

        
        while(n != 0){
            int bit = n&1;
            if( (bit==1) && (index%2==0)){
                
                // ans.push_back(index);       // Even Push krdia h
                // index++;
                
                ans[0]++;
                index++;

            }
            else if( (bit==1) && (index%2 != 0)){
                // Odd index h
               
               ans[1]++;
               index++;
               
            }
            else{
                // bit == 0 case:
                index++;
            }
            n = n>>1;
        }

        return ans;
    }
};
```
['Math', 'Bit Manipulation', 'C++']