Card Trade
InterviewTime limit2sMemory limit512 MB
Given two sorted card piles, find the largest N such that Alice and Beatriz can each give away N cards of distinct kinds that the other does not own.
- Level
Easy3 of 10
- Topics
- Two pointers, Array, Hash map
- Solved
- No attempts yet
Problem
Alice and Beatriz collect Pokemon cards. There is a battle game played with these cards, but the two girls are still small, so they do not play it and care only about the cards themselves. Each card carries one integer number, and two cards with the same number count as the same kind.
Each girl owns a pile of cards, and the two want to trade with each other. Two rules apply. A kind of card that both of them already own is never traded. Also, the cards a girl receives in the trade must not contain the same kind twice. The trade happens in a single operation: Alice hands Beatriz cards of pairwise different kinds, and Beatriz hands Alice cards of pairwise different kinds.
Write a program that finds the largest the two girls can trade.
Input
The input holds several test cases. The first line of a test case holds the number of cards Alice owns, , and the number of cards Beatriz owns, , separated by one space (, ). The second line holds the numbers of Alice's cards, integers separated by spaces (). The third line holds the numbers of Beatriz's cards, integers separated by spaces (). Each girl's card numbers are given in non-decreasing order.
The last line of the input holds two zeros separated by one space. That line is not a test case.
Output
For each test case print one line with the largest number of cards Alice and Beatriz can trade with each other.