-
Notifications
You must be signed in to change notification settings - Fork 0
/
338.py
33 lines (23 loc) · 784 Bytes
/
338.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Problem:
Given an integer n, find the next biggest integer with the same number of 1-bits on.
For example, given the number 6 (0110 in binary), return 9 (1001).
"""
def get_set_bits(num: int) -> int:
# get the number of bits set in a number [runs in O(log(n))]
bin_num = bin(num)[2:]
return sum([int(digit) for digit in bin_num])
def get_next_number_with_same_count_of_set_bits(num: int) -> int:
num_of_set_bits = get_set_bits(num)
curr = num + 1
while True:
if num_of_set_bits == get_set_bits(curr):
return curr
curr += 1
if __name__ == "__main__":
print(get_next_number_with_same_count_of_set_bits(6))
"""
SPECS:
TIME COMPLEXITY: O(n) [as the result always lies between n and 2n]
SPACE COMPLEXITY: O(log(n))
"""