A chain of n relay towers broadcasts on n distinct integer frequencies. A technician injects a signal at a starting frequency, original, into the chain. Whenever the signal's current frequency exactly matches the frequency broadcast by one of the towers, that tower amplifies the signal and doubles its frequency; the resulting frequency is then checked again against every tower, and the doubling repeats for as long as the current frequency keeps matching some tower. The moment the current frequency no longer matches any tower's frequency, the process stops for good. Determine the signal's final frequency.
A single integer: the final frequency of the signal. Because repeated doubling can produce a value far larger than any input bound, print the exact integer value (do not round or use floating point).
Example 1
Input
5 3 5 3 6 1 12
Expected
24
Explanation
Signal starts at 3. 3 matches a tower -> doubles to 6. 6 matches a tower -> doubles to 12. 12 matches a tower -> doubles to 24. 24 matches no tower, so the process stops and the final frequency is 24.
Example 2
Input
4 8 2 4 6 10
Expected
8
Explanation
Signal starts at 8, which does not match any of the towers {2,4,6,10}, so no doubling ever happens and the final frequency is simply 8.
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 →