Find the Union and Intersection of the two sorted arrays in Java
Find the Union & Interaction of the two sorted arrays
Here, in this page we will discuss the program to find the union and interaction of two sorted arrays in Java . We are given with two sorted arrays and we have to find the union and interaction of the given two arrays
Algorithm to find Union :
- Use two index variables i and j, initial values i = 0, j = 0
- If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i.
- If arr1[i] is greater than arr2[j] then print arr2[j] and increment j.
- If both are same then print any of them and increment both i and j.
- Print remaining elements of the larger array.
Algorithm to find Interaction :
- Use two index variables i and j, initial values i = 0, j = 0.
- If arr1[i] is smaller than arr2[j] then increment i.
- If arr1[i] is greater than arr2[j] then increment j.
- If both are same then print any of them and increment both i and j.
Java Code
Run
import java.util.*; public class Main { public static void Union(int[] arr1, int[] arr2) { int i = 0; int j = 0; while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) { System.out.print(arr1[i] + " "); i++; } else if (arr2[j] < arr1[i]) { System.out.print(arr2[j] + " "); j++; } else { System.out.print(arr1[i] + " "); i++; j++; } } while (i < arr1.length) { System.out.print(arr1[i] + " "); i++; } while (j < arr2.length) { System.out.print(arr2[j] + " "); j++; } } public static void Intersection(int[] arr1, int[] arr2) { int i = 0; int j = 0; while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) { i++; } else if (arr1[i] > arr2[j]) { j++; } else { System.out.print(arr1[i] + " "); i++; j++; } } } public static void main(String[] args) throws Exception { Scanner scn = new Scanner(System.in); int n1 = 3; System.out.println("The size of the array 1:- "+n1); int[] arr1 = new int[]{ 1,2,3}; System.out.println("The Elements of array 1"); for (int i = 0; i < n1; i++) { System.out.print(arr1[i] + " "); } System.out.println(); int n2 = 4; System.out.println("The size of the array 2:- "+n2); int[] arr2 = new int[]{4,5,6,7}; System.out.println("The Elements of array 2"); for (int i = 0; i < n2; i++) { System.out.print(arr2[i] + " "); } System.out.println(); System.out.println("The Union of the two array is "); Union(arr1, arr2); System.out.println(); Intersection(arr1, arr2); } }
Output:-
The size of the array 1:- 3
The Elements of array 1
1 2 3
The size of the array 2:- 4
The Elements of array 2
4 5 6 7
The Union of the two array is
1 2 3 4 5 6 7
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
public static void main(String[] args) {
//for union
int[] a={1,6,2,5,6,9,3};
int[] b={2,5,8,2,7,1};
Set s=new TreeSet();
for(int i:a){
s.add(i);
}
for(int i:b){
s.add(i);
}
List ls=new ArrayList(s);
Collections.sort(ls);
for(int i:ls){
System.out.print(i+” “);
}
System.out.println();
//for intersection
Set u=new TreeSet();
List lu=new ArrayList();
for(int i:a){
u.add(i);
}
for(int i:b){
if(u.contains(i)&& !lu.contains(i)){
lu.add(i);
}
}
Collections.sort(lu);
for(int i:lu){
System.out.print(i+” “);
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n1=scn.nextInt();
int[] arr1=new int[n1];
for(int i=0;i<n1;i++){
arr1[i]=scn.nextInt();
}
int n2=scn.nextInt();
int[] arr2=new int[n2];
for(int i=0;i<n2;i++){
arr2[i]=scn.nextInt();
}
int i=0,j=0,k=0;
int[] ans=new int[arr1.length+arr2.length];
while(i<arr1.length && j<arr2.length){
if(arr1[i]arr2[j]){
ans[k]=arr2[j];
j++;
k++;
}
else{
ans[k]=arr1[i];
i++;
j++;
k++;
}
}
while(i<arr1.length){
ans[k]=arr1[i];
i++;
k++;
}
while(j<arr2.length){
ans[k]=arr2[j];
j++;
k++;
}
for(int temp:ans)
System.out.print(temp+" ");
}
}