#K2106. Array Intersection

    ID: 24663 Type: Default 1000ms 256MiB

Array Intersection

Array Intersection

Given two arrays of integers, your task is to compute the intersection of these two arrays such that each element in the result appears only once. The result must be output in sorted order (i.e. in ascending order).

Input Format:

  • The first line contains an integer n, representing the number of elements in the first array.
  • The second line contains n space-separated integers.
  • The third line contains an integer m, representing the number of elements in the second array.
  • The fourth line contains m space-separated integers.

Output Format:

  • Output a single line with the unique intersection numbers sorted in ascending order and separated by a single space. If there is no intersection, output an empty line.

The mathematical formulation of the problem is as follows:

Find \( I = \{ x : x \in A \land x \in B \} \) and output \( I \) sorted in ascending order.

inputFormat

The input is read from standard input (stdin) and contains four lines:

  • Line 1: An integer n denoting the number of elements in the first array.
  • Line 2: n space-separated integers that make up the first array.
  • Line 3: An integer m denoting the number of elements in the second array.
  • Line 4: m space-separated integers that make up the second array.

outputFormat

Output to standard output (stdout) a single line containing the sorted unique intersection of the two arrays. The numbers should be separated by a space. If there is no intersection, print an empty line.

## sample
4
1 2 2 1
2
2 2
2