💯Very Easy Detailed Solution || 🎯 Beats 100% || 🎯 Two Approaches
Approach 1: Brute Force\n Describe your approach to solving the problem. \n1. Helper Function check(String str):\n    - This method checks if a given binary str
# Approach 1: Brute Force\n<!-- Describe your approach to solving the problem. -->\n1. **Helper Function check(String str):**\n    - This method checks if a given binary string contains only the character \'1\'.\n    - If it encounters a \'0\' in the string, it returns false; otherwise, it returns true.\n\n2. **Method smallestNumber(int n):**\n    - Starts with result = n.\n    - Converts the current number (result) to its binary representation using Integer.toBinaryString(result).\n    - Uses the check method to verify if the binary representation contains only 1.\n        -  If true, the loop breaks, and the number is returned.\n        -  Otherwise, result is incremented, and the process repeats.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$O((k-n).log(k))$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$O(log(k))$\n# Code\n```java []\nclass Solution {\n    private boolean check(String str){\n        for(char ch : str.toCharArray()){\n            if(ch == \'0\'){\n                return false;\n            }\n        }\n        return true;\n    }\n    public int smallestNumber(int n) {\n        int result = n;\n        while(true){\n            if(check(Integer.toBinaryString(result))){\n                break;\n            }\n            else{\n                result++;\n            }\n        }\n        return result;\n    }\n}\n```\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe numbers with binary representation containing only 1 are powers of 2-1:\nBinary numbers like 1, 11, 111, etc., correspond to 2^1 - 1, 2^2 - 1, 2^3 - 1, and so on.\nPrecompute these values or derive them directly to reduce brute-force iterations. This would result in logarithmic or constant time checks instead of linear iteration.\n# Approach 2: Optimized Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialization:**\n    - ans is initialized to 1 (binary: 1).\n\n2. **Iterative Multiplication:**\n    - The loop multiplies ans by 2 and adds 1 in each iteration. This operation appends a 1 to the binary representation of ans. For example:\n        - Start with 1 (binary: 1).\n        - Multiply by 2 and add 1: 1\u21923 (binary: 11).\n        - Multiply by 2 and add 1: 3\u21927 (binary: 111).\n        - Multiply by 2 and add 1: 7\u219215 (binary: 1111).\n\n3. **Loop Termination:**\n    - The loop continues until ans is greater than or equal to n. At this point, ans is returned.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$O(log(n))$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$O(1)$\n# Code\n```java []\nclass Solution {\n    public int smallestNumber(int n) {\n        int ans = 1;\n        while(ans < n){\n            ans = ans*2 + 1;\n        }\n        return ans;\n    }\n}\n```
['Math', 'String', 'Bit Manipulation', 'String Matching', 'Bitmask', 'Java']