Skip to content

Commit 82bff4f

Browse files
authored
Merge pull request #1692 from p1kalys/pavan-emani
Added a program in Hash Table.
2 parents 0125ad6 + 3c07abd commit 82bff4f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane.
2+
3+
#We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation.
4+
5+
#Return the number of pairs (i, j) such that i < j and the distance between points i and j is equal to k.
6+
7+
def count_pairs(coordinates, k):
8+
result = 0
9+
seen = {}
10+
11+
for x, y in coordinates:
12+
for split in range(k + 1):
13+
x_complement = x ^ split
14+
y_complement = y ^ (k - split)
15+
result += seen.get((x_complement, y_complement), 0)
16+
seen[x, y] = seen.get((x, y), 0) + 1
17+
18+
return result
19+
20+
# Take user input for coordinates
21+
coordinates = []
22+
num_coordinates = int(input("Enter the number of coordinates: "))
23+
24+
for i in range(num_coordinates):
25+
x = int(input(f"Enter x-coordinate for point {i + 1}: "))
26+
y = int(input(f"Enter y-coordinate for point {i + 1}: "))
27+
coordinates.append((x, y))
28+
29+
# Take user input for k
30+
k = int(input("Enter the value of k: "))
31+
32+
pair_count = count_pairs(coordinates, k)
33+
print(f"Number of pairs that satisfy the condition: {pair_count}")

0 commit comments

Comments
 (0)