-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-2.java
52 lines (36 loc) · 1.05 KB
/
Problem-2.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
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> merge 2 sorted arrays
Problem Link :- https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays5135/1
*/
class Solution {
public void merge(int arr1[], int arr2[], int n, int m) {
// code here
/*
Trick:-
step-1 comparing 2nd array starting(lowest Numbers) with 1st array last (highest numbers) and swapping.
step-2 when all 1st array elements are replaced with smaller once. Then we will sort both the arrays separately.
*/
int i=arr1.length-1;
int j=0;
while(i>=0 && j<=arr2.length-1)
{
if(arr1[i]<=arr2[j])
{
break;
}
else
{
int temp=arr2[j];
arr2[j]=arr1[i];
arr1[i]=temp;
i--;
j++;
}
}
Arrays.sort(arr1);
Arrays.sort(arr2);
}
}