Problems > Common Elements

Common Elements

Difficulty: 1100 3500 ms 128 MB

implementation

Count the elements the two multisets share: for every value, min(count in A, count in B), summed.

Input: the first line has N; the second line has A's N values; the third line has B's N values.

Output: the multiset intersection size.

Constraints:

  • 1 ≤ N ≤ 105
  • |values| ≤ 109

Sample Tests

Input 1
4
1 2 2 3
2 2 4 1
Output 1
3
Input 2
2
5 6
7 8
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.