-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge2SortedArr.java
More file actions
51 lines (43 loc) · 1.33 KB
/
Merge2SortedArr.java
File metadata and controls
51 lines (43 loc) · 1.33 KB
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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import java.util.Scanner;
public class Merge2SortedArr {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scn.nextInt();
}
int n1 = scn.nextInt();
int[] b = new int[n];
for (int i = 0; i < n1; i++) {
b[i] = scn.nextInt();
}
int[] res = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length || j < b.length) {
if (i < a.length && j < b.length) {
if (a[i] < b[j]) {
res[k] = a[i++];
}
else {
res[k] = b[j++];
}
}
else if (i < a.length) {
res[k] = a[i++];
}
else {
res[k] = b[j++];
}
k++;
}
for (int row = 0; row < res.length; row++) {
System.out.print(res[row] + "\t");
}
}
}