Intuition + Approach | Power of 2 | T.C ~ O(logn)
Intuition\n\nIf we subtract 1 from any power of 2 we will get number having all set bits.\n\nExample:-\n\n     2^3 -1 = (8-1) =7 -> binary(111)\n\n## Approach\n
# Intuition\n\nIf we subtract 1 from any power of 2 we will get number having all set bits.\n\nExample:-\n\n     2^3 -1 = (8-1) =7 -> binary(111)\n\n## Approach\n- Find 1st power of 2 which is greater than or equal to n+1\n- Ans is  (Power of 2) -1\n\n        For Example:- \n        n = 5,\n        power of 2 greater than or equal to (n+1 = 6) is 2^3 =8,\n        binary representation of (8-1 = 7) --> 111,\n        Therefore 7 is ans\n\n\n# Complexity\n- Time complexity: O(logn)\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int powerOf2(int n){\n        int p = 1;\n        while(p<n){\n            p*=2;\n        }\n        return p;\n    }\n    public int smallestNumber(int n) {\n        int num = powerOf2(n+1);\n        return num-1;\n    }\n}\n```\n# PLEASE UPVOTE ! THANKS\n.\n.\n\n.
['Java']