-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeergeArr.java
More file actions
43 lines (41 loc) · 1.03 KB
/
MeergeArr.java
File metadata and controls
43 lines (41 loc) · 1.03 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
import java.util.*;
public class MeergeArr {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int [] arr1 = new int[N];
int [] arr2 = new int[M];
for(int i = 0; i < N; i++){
arr1[i] = sc.nextInt();
}
for(int j = 0; j < M; j++){
arr2[j] = sc.nextInt();
}
ArrayList<Integer> ls = new ArrayList<>();
int l = 0;
int r = 0;
while(l < N && r < M){
if(arr2[r] >= arr1[l]){
ls.add(arr2[r]);
r++;
}
else{
ls.add(arr1[l]);
l++;
}
}
while(l < N){
ls.add(arr1[l]);
l++;
}
while(r < M){
ls.add(arr2[r]);
r++;
}
int []res = ls.stream().mapToInt(i -> i).toArray();
for(int num : res){
System.out.print(num + " ");
}
}
}