-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergearray.java
More file actions
58 lines (57 loc) · 1.58 KB
/
Copy pathmergearray.java
File metadata and controls
58 lines (57 loc) · 1.58 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
package array;
import java.util.Scanner;
import java.util.Arrays;
public class mergearray {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n,m;
System.out.println("enter number of element in first array : ");
n=s.nextInt();
int ar[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter the element : "+i);
ar[i]=s.nextInt();
}
Arrays.sort(ar);
System.out.println("enter number of element in second array : ");
m=s.nextInt();
int ar2[]=new int[m];
for(int i=0;i<m;i++)
{
System.out.println("enter the element : "+i);
ar2[i]=s.nextInt();
}
Arrays.sort(ar2);
int ar3[]=new int[n+m];
int a=0,b=0,c=0;
while(a<n)
{
ar3[c++]=ar[a++];
}
while(b<m)
{
ar3[c++]=ar2[b++];
}
Arrays.sort(ar3);
for(int i=0;i<m+n;i++)
{
System.out.print(ar3[i]+" ");
}
System.out.println();
if(ar3.length%2!=0)
{
for(int i=ar3.length/2-1;i<=ar3.length/2;i++)
{
System.out.println(ar3[i]);
}
}
else{
for(int i=ar3.length/2;i<=ar3.length/2;i++)
{
int sum=(ar3[i-1]+ar3[i])/2;
System.out.println("the median is : "+sum);
}
}
}
}