forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path317.py
More file actions
30 lines (22 loc) · 595 Bytes
/
317.py
File metadata and controls
30 lines (22 loc) · 595 Bytes
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
"""
Problem:
Write a function that returns the bitwise AND of all integers between M and N,
inclusive.
"""
def bitwise_and_on_range(start: int, end: int) -> int:
# using naive approach
result = start
for num in range(start + 1, end + 1):
result = result & num
return result
if __name__ == "__main__":
print(bitwise_and_on_range(3, 4))
print(bitwise_and_on_range(5, 6))
print(bitwise_and_on_range(126, 127))
print(bitwise_and_on_range(127, 215))
print(bitwise_and_on_range(129, 215))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""