- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathMergeTwoSortedArrays.java
121 lines (102 loc) · 3.32 KB
/
MergeTwoSortedArrays.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Merge Two Sorted Arrays
// You have been given two sorted arrays/lists(ARR1 and ARR2) of size N and M respectively, merge them into a third array/list such that the third array is also sorted.
// Input Format :
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
// First line of each test case or query contains an integer 'N' representing the size of the first array/list.
// Second line contains 'N' single space separated integers representing the elements of the first array/list.
// Third line contains an integer 'M' representing the size of the second array/list.
// Fourth line contains 'M' single space separated integers representing the elements of the second array/list.
// Output Format :
// For each test case, print the sorted array/list(of size N + M) in a single row, separated by a single space.
// Output for every test case will be printed in a separate line.
// Constraints :
// 1 <= t <= 10^2
// 0 <= N <= 10^5
// 0 <= M <= 10^5
// Time Limit: 1 sec
// Sample Input 1 :
// 1
// 5
// 1 3 4 7 11
// 4
// 2 4 6 13
// Sample Output 1 :
// 1 2 3 4 4 6 7 11 13
// Sample Input 2 :
// 2
// 3
// 10 100 500
// 7
// 4 7 9 25 30 300 450
// 4
// 7 45 89 90
// 0
// Sample Output 2 :
// 4 7 9 10 25 30 100 300 450 500
// 7 45 89 90
packageArray;
importjava.util.Scanner;
publicclassMergeTwoSortedArrays {
publicstaticint [] Array1() {
Scannersc = newScanner(System.in);
System.out.println("Enter the length of the array1: ");
intn = sc.nextInt();
int [] arr = newint[n];
System.out.println("Enter the elements of the array1: ");
for (inti = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
returnarr;
}
publicstaticint [] Array2 () {
Scannersc = newScanner(System.in);
System.out.println("Enter the length of the array2: ");
intn = sc.nextInt();
int [] arr = newint[n];
System.out.println("Enter the elements of the array2: ");
for (inti = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
returnarr;
}
publicstaticint [] mergeArray(int [] arr1, int [] arr2) {
intm = arr1.length;
intn = arr2.length;
int [] arr3 = newint[m + n];
inti = 0, j = 0, k = 0;
while ( i < m && j < n) {
if (arr1[i] <= arr2[j]) {
arr3[k] = arr1[i];
k++;
i++;
} else {
arr3[k] = arr2[j];
j++;
k++;
}
}
while ( i < m) {
arr3[k] = arr1[i];
i++;
k++;
}
while (j < n) {
arr3[k] = arr2[j];
j++;
k++;
}
returnarr3;
}
publicstaticvoidprintArray(int [] arr) {
System.out.print("The merged array is: ");
for (inti = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
publicstaticvoidmain(String[] args) {
int [] arr1 = Array1();
int [] arr2 = Array2();
int [] arr3 = mergeArray(arr1, arr2);
printArray(arr3);
}
}