Number Complement - Beats 100% - Unique and Easy solution 🌟🔜
Intuition\nIf we find remainder when divided the number by 2,it\'ll give the bit representation in reverse order.With that ,we\'ll calculate the complement of t
# Intuition\nIf we find remainder when divided the number by 2,it\'ll give the bit representation in reverse order.With that ,we\'ll calculate the complement of the number by converting that binary representation to integer by multiplying with powers of 2 if the remainder obtained is 0.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n - The idea is to apply the technique to convert decimal to binary.\n - Initialize newnum to 0 which is the complement of the given number.\n - So find the remainder of the given number when divided by 2.\n - With that remainder,if it is \'0\' ,to find the complement ,actually it must be changed to 1.\n - Add to the newnum,the powers of 2 with initializing base (In our case k to 0),then at iteration multiply k by 2.\n  - Then to find next bit of the number from right,divide the number by 2 in each iteration. \n  - The iteration is repeated till the number is greater than 0.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:**O(log n)**.\n  n ->value of the number num.\n- The iteration repeats till the number of bits of n which is proportional to **log2(n)**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**O(1)**.\n- No additional datastructure doesn\'t require hence the space required doesn\'t depend on input size.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int findComplement(int num) {\n        int newnum=0;\n        int k=1;\n        while(num>0)\n        {\n            int r=num%2;\n            //System.out.println(r);\n            if(r==0)\n            {\n                \n                newnum+=k;\n                \n            }\n            k*=2;\n            num=num/2;\n        }\n       \n        return newnum;\n\n    }\n}\n```
['Bit Manipulation', 'Java']