Advertisement
Egonau

1.5-2

Jan 7th, 2023
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int l1 = sc.nextInt();
  8.         int[] a1 = new int[l1];
  9.         for(int i = 0; i<l1;++i){
  10.             a1[i] = sc.nextInt();
  11.         }
  12.         int l2 = sc.nextInt();
  13.         int[] a2 = new int[l2];
  14.         for(int i = 0; i<l2;++i){
  15.             a2[i] = sc.nextInt();
  16.         }
  17.         int[] anew = new int[l1+l2];
  18.         int c1 = 0;
  19.         int c2 = 0;
  20.         for (int i = 0;i<anew.length;++i){
  21.             if (a1[c1]==a2[c2]){
  22.                 anew[i]=a1[c1];
  23.                 i+=1;
  24.                 anew[i] = a2[c2];
  25.                 if (c1<l1-1){ c1+=1; }
  26.                 else{ a1[c1] = Integer.MAX_VALUE; }
  27.                 if (c2<l2-1){ c2+=1; }
  28.                 else{ a2[c2] = Integer.MAX_VALUE; }
  29.             }
  30.             else {
  31.                 if (a1[c1]>a2[c2]){
  32.                     anew[i]=a2[c2];
  33.                     if (c2<l2-1){ c2+=1; }
  34.                     else{ a2[c2] = Integer.MAX_VALUE; }
  35.                 }
  36.                 else{
  37.                     anew[i]=a1[c1];
  38.                     if (c1<l1-1){ c1+=1; }
  39.                     else{ a1[c1] = Integer.MAX_VALUE; }
  40.                 }
  41.             }
  42.         }
  43.         for(int i = 0;i< anew.length;++i){
  44.             System.out.print(anew[i]+" ");
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement