A survey satellite orbits above a ring of unmanned relay stations spaced around a perfectly circular patrol path. Every station continuously broadcasts a fixed numeric ID that never changes, and no two stations on the ring share the same ID. As the satellite completes lap after lap without ever stopping, its ground receiver appends the ID it hears at each moment to a single growing log, in the exact order the stations were passed. You are handed a finite prefix of that log — long enough to cover at least one full lap, and always cut off exactly at the end of a lap, never partway through one. Recover the one piece of information mission control actually needs: how many relay stations are on the ring.
m, the number of entries in the log.m space-separated integers, the station IDs in the order the satellite radioed them, starting from an arbitrary station and continuing for one or more complete laps.Print a single integer: the number of relay stations on the ring.
n satisfies 1 <= n <= 1000, that the log's first n entries are pairwise distinct (one per station), and that m is an exact positive-integer multiple of n — the log always ends exactly at a lap boundary and never partway through one.Example 1
Input
6 5 9 2 5 9 2
Expected
3
Explanation
The satellite radios back IDs 5, 9, 2 and then the exact same sequence 5, 9, 2 again, so it has completed two identical laps around a ring of 3 stations. The minimal repeating block has length 3, so the answer is 3.
Example 2
Input
4 7 7 7 7
Expected
1
Explanation
Every single reading in the log is the same ID, 7. That means the satellite passes the same one station on every step, so the ring holds only 1 station and the 4 log entries are 4 consecutive laps around it.
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 →