Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int l1 = sc.nextInt();
- int[] a1 = new int[l1];
- for(int i = 0; i<l1;++i){
- a1[i] = sc.nextInt();
- }
- int l2 = sc.nextInt();
- int[] a2 = new int[l2];
- for(int i = 0; i<l2;++i){
- a2[i] = sc.nextInt();
- }
- int[] anew = new int[l1+l2];
- int c1 = 0;
- int c2 = 0;
- for (int i = 0;i<anew.length;++i){
- if (a1[c1]==a2[c2]){
- anew[i]=a1[c1];
- i+=1;
- anew[i] = a2[c2];
- if (c1<l1-1){ c1+=1; }
- else{ a1[c1] = Integer.MAX_VALUE; }
- if (c2<l2-1){ c2+=1; }
- else{ a2[c2] = Integer.MAX_VALUE; }
- }
- else {
- if (a1[c1]>a2[c2]){
- anew[i]=a2[c2];
- if (c2<l2-1){ c2+=1; }
- else{ a2[c2] = Integer.MAX_VALUE; }
- }
- else{
- anew[i]=a1[c1];
- if (c1<l1-1){ c1+=1; }
- else{ a1[c1] = Integer.MAX_VALUE; }
- }
- }
- }
- for(int i = 0;i< anew.length;++i){
- System.out.print(anew[i]+" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement