🚀 Beats 100% Runtime, 99.83% in Memory - The Simplest Solution! 🌟
Intuition\n Describe your first thoughts on how to solve this problem. \nThe task is to count the number of 1\'s in the binary representation of an integer at e
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe task is to count the number of 1\'s in the binary representation of an integer at even and odd positions.\n\n# Approach\n**Step-by-Step Guide to Count Even and Odd Bits**\n\n1.***Check LSB (Least Significant Bit):*** \nPerform a bitwise AND operation of the number num with 1. If the result of num & 1 is not equal to 0, it means the LSB of num is 1; otherwise, it is 0.\nFor example:\n5 (0101) & 1 (0001) = 0001 (not equal to 0)\n\n**2**.***Increment Even and Odd Counts:*** \nBased on the result from the previous step and the current position i (even or odd), increment the corresponding count variable even or odd.\n\nFor example, if the result of num & 1 is not equal to 0 and i is even, increment the even count. Similarly, if i is odd, increment the odd count.\n\n**3**.***Update num for Next Iteration:** \nRight shift the number num by 1 position to move to the next bit. Update the value of num with the result of the right shift.*\n\nFor example:\n5 (0101) >> 1 = 0010\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- **Time complexity: O(1)**\n- The time complexity of the given code is O(1), which means it has **constant time complexity.** Regardless of the input value n, **the while loop runs 32 times** because an int in Java is represented by 32 bits. Therefore, the **number of iterations is fixed** and not dependent on the size of n.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- **Space complexity:O(1)**\n- The space complexity of the code is O(1) as well. The code uses a constant amount of space to store the integers even, odd, and i, as well as the integer array created using new int[]{even, odd}. **The space used does not depend on the input value \'n\'.**\n\n# Code\n```\nclass Solution {\n    public int[] evenOddBit(int n) {\n\n        int even =0;\n        int odd =0;\n        int i=0;\n\n        while(i<32)\n        {\n            if( ( (n & 1)!=0 ) && (i%2 ==0) )\n                even ++;\n            else\n                if( ( (n & 1) !=0) && (i%2 != 0) )\n                    odd++;\n                \n            n = n>>1;\n            i++;\n        }\n        return new int[]{even,odd};\n\n    }\n}\n```
['Bit Manipulation', 'Java']