Problem of The Day: Smallest Number With All Set Bits
Problem Statement
Brute Force [TLE]
class Solution:
def smallestNumber(self, n: int) -> int:
x = n
res = 0
while x > 0:
res = (res << 1) | 1
x = x >> 1
return res
Editorial
class Solution:
def smallestNumber(self, n: int) -> int:
x = 1
while x < n:
x = x * 2 + 1
return x