Code to find complement of Base 10 integer
Intuition\nIn this question we have to find the complement of the given number. So we will simply do bitwise complement of the given number.\nFor eg., let x=100
# Intuition\nIn this question we have to find the complement of the given number. So we will simply do bitwise complement of the given number.\nFor eg., let x=10011 so it\'s complement would be 01100.\n\n# Approach\nWe wil initialize few variables in the starting that are mul and sum for storing the power of 2\'s and the resultant number respectively.\n\nNow, we will iterate the loop untill it is less than 0 .\n\nAnd inside the while loop we will simply find the remainder when the given number is divided by 2.\n\nThen, change the digit to 0 and 1 if it would be 1 and 0 respectively.\n\nThen, reduce the number by n/2 and store it in sum variable after performing some calculations.\n\nAnd we also have to change the mul variable as it is used for 2^0 , 2^1..........2^n. \n\nfor eg., Let x=101 so 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5\n\n# Complexity\n- Time complexity:\nO(log N) because while loop will iterate till the n becomes less than 0 . And it will jump in N/2 manner.\n\n- Space complexity:\nO(1) as only constant space is used in the code.\n\n# Code\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        int rem,ans=0,mul=1;\n        if(n==0)\n        return 1;\n        while(n)\n        {\n            rem=n%2;\n            // rem=rem^1;\n            rem =!rem;\n            n/=2;\n            ans=ans+rem*mul;\n            mul*=2;\n        }\n        return ans;\n        \n        \n    }\n};\n```
['C++']