-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.java
More file actions
58 lines (54 loc) · 1.19 KB
/
intersection.java
File metadata and controls
58 lines (54 loc) · 1.19 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
import java.util.*;
class Intersection {
public static void main(String[] args) {
Scanner o=new Scanner(System.in);
int n,i,j,max=0,k=0,n1;
System.out.println("Enter the size of array 1");
n=o.nextInt();
int arr[]=new int[n];
System.out.println("Enter elements of array 1");
for(i=0;i<n;i++)
{
arr[i]=o.nextInt();
}
System.out.println("Enter the size of array 2");
n1=o.nextInt();
int arr1[]=new int[n1];
System.out.println("Enter elements of array 2");
for(i=0;i<n1;i++)
{
arr1[i]=o.nextInt();
}
int arr2[]=new int[20];
for(i=0;i<n;i++)
{
for(j=0;j<n1;j++)
{
if((arr[i]==arr1[j])&&(arr[i]!=-1)&&(arr1[j]!=-1))
{
arr2[k]=arr[i];
k++;
arr[i]=-1;
arr1[j]=-1;
}
}
}
for(i=0;i<k;i++)
{
for(j=0;j<k-i-1;j++)
{
if(arr2[j]>arr2[j+1])
{
arr2[j]=arr2[j]^arr2[j+1];
arr2[j+1]=arr2[j]^arr2[j+1];
arr2[j]=arr2[j]^arr2[j+1];
}
}
}
System.out.println("final array:");
for(i=0;i<k;i++)
{
System.out.print(arr2[i]+" ");
}
}
}