-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_09.py
84 lines (57 loc) · 1.9 KB
/
Day_09.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
DAY 9: Union of Two Sorted Arrays.
https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/
QUESTION : Union of two arrays can be defined as the common and distinct
elements in the two arrays.
Given two sorted arrays of size N and M respectively, find their union.
Expected Time Complexity: O(N+M).
Expected Auxiliary Space: O(N+M).
Constraints:
1 <= N,M <= 10^5
1 <= arr1[i], arr2[i] <= 10^6
"""
def union(arr1, arr2, m, n):
union_arr = []
i=j=k=0
while i<m and j<n:
if k>0:
if arr1[i] < arr2[j]:
if union_arr[-1] != arr1[i] and k>0:
union_arr.append(arr1[i])
i+=1
elif arr1[i] > arr2[j]:
if union_arr[-1] != arr2[j] and k>0:
union_arr.append(arr2[j])
j+=1
else:
if union_arr[-1] != arr1[i] and k>0:
union_arr.append(arr1[i])
i+=1
j+=1
k+=1
else:
if arr1[i] < arr2[j]:
union_arr.append(arr1[i])
i+=1
elif arr1[i] > arr2[j]:
union_arr.append(arr2[j])
j+=1
else:
union_arr.append(arr1[i])
i+=1
j+=1
k+=1
while i<m:
if union_arr[-1] != arr1[i] and k>0:
union_arr.append(arr1[i])
i+=1
k+=1
while j<n:
if union_arr[-1] != arr2[j] and k>0:
union_arr.append(arr2[j])
j+=1
k+=1
return union_arr
lst1 = [4, 12, 45]
lst2 = [13, 17, 18, 19, 20, 22, 22, 27, 36, 39, 46, 48, 50]
print(union(lst1, lst2, len(lst1), len(lst2)))