Skip to content

Commit c9473a8

Browse files
author
Amogh Singhal
authored
Create intersection_arrays.py
1 parent 96b17ec commit c9473a8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

intersection_arrays.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Problem: Given two sorted array of sizes m and n
2+
# in which all elements are distinct. Find the
3+
# common elements between them
4+
# Constraints: in O(m+n) complexity.
5+
6+
def inr(z):
7+
return z+1
8+
9+
def intersectionArrays(x, y, m, n):
10+
i, j = 0, 0
11+
while i<m and j<n:
12+
#print(i, j)
13+
if(x[i] == y[j]):
14+
print(x[i])
15+
i = inr(i)
16+
j = inr(j)
17+
elif(x[i] < y[j]):
18+
i = inr(i)
19+
else:
20+
j = inr[j]
21+
22+
list_a = [1, 2, 3, 4, 5]
23+
list_b = [2, 3, 5, 6]
24+
intersectionArrays(list_a, list_b, len(list_a), len(list_b))

0 commit comments

Comments
 (0)