✔💯 DAY 363 || veryyyy easy || all ways || explained || ONE PASS || BIT MANIPULATION
Intuition & Approach &  Complexity\n##### \u2022\tThe problem asks us to count the number of 1\'s in the binary representation of a given positive integer n at 
# Intuition & Approach &  Complexity\n##### \u2022\tThe problem asks us to count the number of 1\'s in the binary representation of a given positive integer n at even and odd indices separately. \n##### \u2022\tThe even indices are the positions with an even power of 2, starting from the rightmost bit with index 0, while the odd indices are the positions with an odd power of 2. \n##### \u2022\tThe evenOddBit1() function uses a simple approach to count the number of 1\'s at even and odd indices separately. It iterates over the bits of n from right to left, and for each bit, it checks if it is at an even or odd index based on the value of the flag variable. \n##### \u2022\tIf the bit is at an even index, it increments the even counter, otherwise, it increments the odd counter. It then toggles the value of the flag variable to switch between even and odd indices, and shifts n to the right by 1 bit. \n##### \u2022\tThe algorithm used in the evenOddBit1() function is based on the observation that the binary representation of a positive integer n has a 1 at an even index if and only if the least significant bit of n is 1, and the binary representation of n/2 has a 1 at an odd index. \n##### \u2022\tSimilarly, the binary representation of n has a 1 at an odd index if and only if the least significant bit of n is 1, and the binary representation of n/2 has a 1 at an even index. \n##### \u2022\tThis observation allows us to count the number of 1\'s at even and odd indices separately by iterating over the bits of n and toggling the value of the flag variable. \n##### \u2022\tThe time complexity of the evenOddBit1() function is O(log n), where n is the input integer, since we need to iterate over the bits of n . \n##### \u2022\tThe space complexity is O(1), since we only need to store two integer variables even and odd .\n\n\n# Code\n```java []\n    public int[] evenOddBit1(int n) {\n        int even=0,odd=0;\n        boolean flag=true;\n        while(n>0){\n            if(flag)  even+=n&1;\n            else  odd+=n&1;\n            flag=!flag;\n            n>>=1;\n        }return new int[]{even,odd};\n    }\n```\n```c++ []\nvector<int> evenOddBit(int n) {\n    int even = 0, odd = 0;\n    bool flag = true;\n    while (n > 0) {\n        if (flag) {\n            even += n & 1;\n        } else {\n            odd += n & 1;\n        }\n        flag = !flag;\n        n >>= 1;\n    }\n    return {even, odd};\n}\n```\n```python []\ndef evenOddBit1(n):\n    even, odd = 0, 0\n    flag = True\n    while n > 0:\n        if flag:\n            even += n & 1\n        else:\n            odd += n & 1\n        flag = not flag\n        n >>= 1\n    return [even, odd]\n```\n# 2nd way \n\n```java []\n    public int[] evenOddBit(int n) {\n        int A[] = new int[2],i=0;\n        while(n>0){\n            A[i]+=n%2;\n            i=1-i;\n            n/=2;\n        }return A;\n    }\n```\n```PYTHON []\ndef evenOddBit(n):\n    A = [0, 0]\n    i = 0\n    while n > 0:\n        A[i] += n % 2\n        i = 1 - i\n        n //= 2\n    return A\n```\n```C++ []\nvector<int> evenOddBit(int n) {\n    std::vector<int> A(2);\n    int i = 0;\n    while (n > 0) {\n        A[i] += n % 2;\n        i = 1 - i;\n        n /= 2;\n    }\n    return A;\n}\n```\n\n# ONE LINER \n\n```JAVA []\npublic int[] evenOddBit(int n) {\n    return new int[] {Integer.bitCount(n & 0b0101010101), Integer.bitCount(n & 0b1010101010)};\n}\n```\n\n```C++ []\nvector<int> evenOddBit(int n) {\n    return {__builtin_popcount(n & 0b0101010101), __builtin_popcount(n & 0b1010101010)};\n}\n```\n```PYTHON []\ndef evenOddBit(n):\n    return [bin(n & 0b0101010101).count(\'1\'),  bin(n & 0b1010101010).count(\'1\')]\n```\n\n![BREUSELEE.webp](https://assets.leetcode.com/users/images/a07b9197-a4cc-4232-84b7-9c033153ebe1_1680072290.883467.webp)\n\n![meme2.png](https://assets.leetcode.com/users/images/54f32d08-fa09-4104-9b14-657709a369db_1680072303.138686.png)\n\n
['Bit Manipulation', 'Python', 'C++', 'Java', 'Python3']