Write
a program which input natural number N and M followed by integer arrays A[] and
B[], each consisting of N and M number of elements respectively.
Sort the arrays A[] and B[] in descending
order of magnitude.
Use the sorted arrays to generate a merged
array C[]. Array C[] should be generated in descending order.
Assume that input arrays comprise maximum 20
elements each, with no duplicates.
Common elements should be included in the
merged array only once.
The output format is as follows:-
SORTED ARRAYS A[ ] B[ ]
MERGED ARRAY C[ ]
The logic is to after taking input, sort both the array A and B using
any standard sorting technique. the add the lements of A in C.
Following this add the elements of B in C while checking whether
it is already present in C or not and increasing the counter or not based on whether
the element is not present or is present.
the element is not present or is present.
Then just sort C to ge the result.
import java.util.*;
public class ISC2001Q2 {
public static void main(String[] args) {
int M,N;
Scanner in = new Scanner (System.in);
System.out.println("Enter the numebr of elemenst in Array A");
M = in.nextInt();
while(M >
20) {
System.out.println("INVALID INPUT, TRY AGAIN");
M = in.nextInt();
}
System.out.println("Enter the numebr of elemenst in Array B");
N= in.nextInt();
while(N >
20) {
System.out.println("INVALID INPUT, TRY AGAIN");
N = in.nextInt();
}
MergeArray obj = new MergeArray(M,N);
obj.inputArray();
obj.mergeArray();
in.close();
}
}
class MergeArray{
public void mergeArray() {
sortArray(arrayA,order_A);
sortArray(arrayB,order_B);
System.out.println("Array A after sorting:");
display(arrayA,order_A);
System.out.println("Array B after sorting:");
display(arrayB,order_B);
int counter;
boolean flag;
//adding
elements of A in C
for(int i=0;i<order_A;i++)
arrayC[i]=arrayA[i];
counter=order_A;
// adding
elements of B in C
for(int j=0;j<order_B;j++) {
flag = false;
for(int k=0;k<order_A;k++) {
if(arrayB[j]==arrayC[k]) // checking whether element is
already present
flag = true;
}
if(flag==false)
arrayC[counter++]=arrayB[j];
}
System.out.println("Array C after merging");
sortArray(arrayC,counter);
display(arrayC,counter);
}
private void sortArray(int[] arr,int order) {
int temp;
for(int i = 0; i < order; i++) {
for(int j=1; j < (order-i); j++) {
if(arr[j-1] < arr[j]) {
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
private void display(int[] arr,int order) {
for(int i=0;i<order;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public void inputArray() {
int i;
Scanner in = new Scanner (System.in);
System.out.println("Enter the elements of array A:");
for(i=0;i<order_A;i++)
arrayA[i]=in.nextInt();
System.out.println("Enter the elements of array B:");
for(i=0;i<order_B;i++)
arrayB[i]=in.nextInt();
in.close();
}
MergeArray(int M,int N){
order_A = M;
order_B = N;
arrayA = new int[order_A];
arrayB = new int[order_B];
arrayC = new int[order_A+order_B];
}
private int order_A;
private int order_B;
private int[] arrayA;
private int[] arrayB;
private int[] arrayC;
}
No comments:
Post a Comment