less than 1 minute read

1 min read 153 words

Problem Statement

leetcode problem link

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

Leave a comment