-
Notifications
You must be signed in to change notification settings - Fork 317
/
Code Merge Two Sorted Arrays17609
45 lines (44 loc) · 1.1 KB
/
Code Merge Two Sorted Arrays17609
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
Code Merge Two Sorted Arrays
Given two sorted arrays of Size M and N respectively, merge them into a third array such that the third array is also sorted.
Input Format :
Line 1 : Size of first array i.e. M
Line 2 : M elements of first array separated by space
Line 3 : Size of second array i.e. N
Line 2 : N elements of second array separated by space
Output Format :
M + N integers i.e. elements of third sorted array separated by spaces.
Constraints :
1 <= M and N <= 10^6
Sample Input :
5
1 3 4 7 11
4
2 4 6 13
Sample Output :
1 2 3 4 4 6 7 11 13
def MergeSortedArray(arr1,arr2):
i=0
j=0
len1=len(arr1)
len2=len(arr2)
arr=[ ]
while((i<len1) and (j<len2)):
if(arr1[i]<arr2[j]):
arr.append(arr1[i])
i=i+1
else:
arr.append(arr2[j])
j=j+1
while(i<len1):
arr.append(arr1[i])
i=i+1
while(j<len2):
arr.append(arr2[j])
j=j+1
return arr
n1=int(input())
arr1=[int(x) for x in input().split()]
n2=int(input())
arr2=[int(x) for x in input().split()]
arr=MergeSortedArray(arr1,arr2)
print(*arr)