forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path332.py
More file actions
31 lines (22 loc) · 558 Bytes
/
332.py
File metadata and controls
31 lines (22 loc) · 558 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
31
"""
Problem:
Given integers M and N, write a program that counts how many positive integer pairs
(a, b) satisfy the following conditions:
a + b = M
a XOR b = N
"""
def get_count(M: int, N: int) -> int:
count = 0
for i in range(1, M):
# (a, b) and (b, a) are considered different entities.
# To consider them only once, use range(1, M // 2)
if i ^ (M - i) == N:
count += 1
return count
if __name__ == "__main__":
print(get_count(100, 4))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""