-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.mergeTwoSortedArrayWithoutExtraSpace.java
More file actions
94 lines (83 loc) · 2.81 KB
/
12.mergeTwoSortedArrayWithoutExtraSpace.java
File metadata and controls
94 lines (83 loc) · 2.81 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
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
// { Driver Code Starts
import java.io.*;
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
String inputLine[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int m = Integer.parseInt(inputLine[1]);
int arr1[] = new int[n];
int arr2[] = new int[m];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr1[i] = Integer.parseInt(inputLine[i]);
}
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < m; i++) {
arr2[i] = Integer.parseInt(inputLine[i]);
}
new Solution().merge(arr1, arr2, n, m);
StringBuffer str = new StringBuffer();
for (int i = 0; i < n; i++) {
str.append(arr1[i] + " ");
}
for (int i = 0; i < m; i++) {
str.append(arr2[i] + " ");
}
System.out.println(str);
}
}
}// } Driver Code Ends
class Solution {
// public void merge(int arr1[], int arr2[], int n, int m) {
// // the idea is put all smaller iin one array and and maximum in second array
// int i = arr1.length-1;
// int j =0;
// while(i>=0 && j<arr2.length){
// if(arr1[i]<=arr2[j]){
// break;
// }
// // if 1st ka largest 2nd ka smallest se jyada hai to swap
// else{
// int temp = arr1[i];
// arr1[i] = arr2[j];
// arr2[j] = temp;
// }
// i--;
// j++;
// //if 1st ka max 2nd ke min se chota hai ya brabar to aage kuch karne ki jarurat na hai
// }
// // now sort both arrays
// Arrays.sort(arr1);
// Arrays.sort(arr2);
// }
// }
class Solution {
public void merge(int arr1[], int arr2[], int n, int m) {
// the idea is put all smaller iin one array and and maximum in second array
int i = arr1.length-1;
int j =0;
while(i>=0 && j<arr2.length){
if(arr1[i]<=arr2[j]){
break;
}
// if 1st ka largest 2nd ka smallest se jyada hai to swap
if(arr1[i]>arr2[j]){
int temp = arr1[i];
arr1[i] = arr2[j];
arr2[j] = temp;
}
i--;
j++;
//if 1st ka max 2nd ke min se chota hai ya brabar to aage kuch karne ki jarurat na hai
}
// now sort both arrays
Arrays.sort(arr1);
Arrays.sort(arr2);
}
}