Easy Solution | Beats 100%
Code\npython []\nclass Solution(object):\n    def findComplement(self, num):\n        def decimal_to_binary(n):\n            if n == 0:\n                return 
# Code\n```python []\nclass Solution(object):\n    def findComplement(self, num):\n        def decimal_to_binary(n):\n            if n == 0:\n                return "0"\n            binary = ""\n            while n > 0:\n                binary = str(n % 2) + binary\n                n = n // 2\n            return binary\n\n        def binary_to_decimal(binary_str):\n            decimal = 0\n            binary_str = binary_str[::-1]  # Reverse the string to process from right to left\n            for i in range(len(binary_str)):\n                decimal += int(binary_str[i]) * (2 ** i)\n            return decimal\n\n        binary = decimal_to_binary(num)\n        n = len(binary)\n\n        complement = \'\'\n        for i in range(n):\n            if binary[i] == \'0\':\n                complement += \'1\'\n            else:\n                complement += \'0\'\n\n        return binary_to_decimal(complement)        \n```\n```C++ []\nclass Solution {\npublic:\n    int findComplement(int num) {\n        auto decimal_to_binary = [](int n) {\n            if (n == 0) return std::string("0");\n            std::string binary = "";\n            while (n > 0) {\n                binary = std::to_string(n % 2) + binary;\n                n = n / 2;\n            }\n            return binary;\n        };\n\n        auto binary_to_decimal = [](const std::string &binary_str) {\n            int decimal = 0;\n            int n = binary_str.length();\n            for (int i = 0; i < n; ++i) {\n                if (binary_str[n - i - 1] == \'1\') {\n                    decimal += (1 << i);  // Equivalent to 2^i\n                }\n            }\n            return decimal;\n        };\n\n        std::string binary = decimal_to_binary(num);\n        int n = binary.length();\n\n        std::string complement = "";\n        for (int i = 0; i < n; ++i) {\n            if (binary[i] == \'0\') {\n                complement += \'1\';\n            } else {\n                complement += \'0\';\n            }\n        }\n\n        return binary_to_decimal(complement);\n    }\n};\n```\n```Java []\nclass Solution {\n    public int findComplement(int num) {\n        String binary = decimalToBinary(num);\n        int n = binary.length();\n\n        StringBuilder complement = new StringBuilder();\n        for (int i = 0; i < n; i++) {\n            if (binary.charAt(i) == \'0\') {\n                complement.append(\'1\');\n            } else {\n                complement.append(\'0\');\n            }\n        }\n\n        return binaryToDecimal(complement.toString());\n    }\n\n    private String decimalToBinary(int n) {\n        if (n == 0) return "0";\n        StringBuilder binary = new StringBuilder();\n        while (n > 0) {\n            binary.insert(0, n % 2);\n            n = n / 2;\n        }\n        return binary.toString();\n    }\n\n    private int binaryToDecimal(String binaryStr) {\n        int decimal = 0;\n        int n = binaryStr.length();\n        for (int i = 0; i < n; i++) {\n            if (binaryStr.charAt(n - i - 1) == \'1\') {\n                decimal += Math.pow(2, i);\n            }\n        }\n        return decimal;\n    }\n}\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/9cfd076f-b4cb-417e-a1b6-3c21b11790f1_1724285707.7464666.jpeg)\n
['Bit Manipulation', 'Python', 'C++', 'Java', 'Python3']