Two scouting teams for a robotics league, Team A and Team B, have each ranked the same pool of eligible robot models by how eager they are to feature them in a joint showcase. Team A's ranking is a list of model codes ordered from most wanted (index 0) to least wanted; Team B's ranking is a separate list drawn from the same style of pool, also ordered from most wanted (index 0) to least wanted. A given model code may appear in one ranking, both rankings, or neither.
To pick the fairest joint showcase entrant, the organizers want the model code(s) that appear in BOTH rankings with the smallest possible sum of their two 0-indexed positions -- a low combined rank means both teams are eager to feature it early. If several model codes tie for the smallest sum, all of them must be reported.
It is guaranteed the two rankings share at least one model code in common.
Line 1: two integers n and m -- the number of model codes in Team A's ranking and in Team B's ranking, respectively.
Line 2: n space-separated model codes, Team A's ranking from index 0 (most wanted) to index n-1 (least wanted).
Line 3: m space-separated model codes, Team B's ranking from index 0 (most wanted) to index m-1 (least wanted).
Print every model code that attains the minimum combined rank sum, one per line, in the order each code appears in Team A's ranking (index 0 first). Print nothing else.
Example 1
Input
3 3 falcon viper cobra cobra falcon viper
Expected
falcon
Explanation
Team A: falcon=0, viper=1, cobra=2. Team B: cobra=0, falcon=1, viper=2. Combined sums: falcon=0+1=1, viper=1+2=3, cobra=2+0=2. The minimum sum is 1, attained only by falcon, so falcon is printed.
Example 2
Input
2 2 shadow raptor raptor shadow
Expected
shadow raptor
Explanation
Team A: shadow=0, raptor=1. Team B: raptor=0, shadow=1. Combined sums: shadow=0+1=1, raptor=1+0=1. Both tie for the minimum sum of 1, so both are printed, in Team A's order: shadow then raptor.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →