Skip to content

Commit

Permalink
Added solution 310
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Sep 25, 2020
1 parent 375ef27 commit f0aadf4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Problems/310.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROBLEM 310:

Write an algorithm that finds the total number of set bits in all integers between 1 and N.
37 changes: 37 additions & 0 deletions Solutions/310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Problem:
Write an algorithm that finds the total number of set bits in all integers between 1
and N.
"""


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_total_set_bits(N: int) -> int:
# sums up the number of bits set in all positive numbers till N
result = 0
for i in range(1, N + 1):
result += get_set_bits(i)
return result


if __name__ == "__main__":
print(get_total_set_bits(0))
print(get_total_set_bits(1))
print(get_total_set_bits(2))
print(get_total_set_bits(3))
print(get_total_set_bits(4))
print(get_total_set_bits(5))


"""
SPECS:
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(1)
"""

0 comments on commit f0aadf4

Please sign in to comment.