Global Warming
InterviewTime limit2sMemory limit512 MB
Given a sequence of temperatures, find the length of the shortest period of the sequence of consecutive differences, allowing the final repetition to be truncated.
- Level
Easy3 of 10
- Topics
- String matching, Brute force, Implementation, Math
- Solved
- No attempts yet
Problem
Scientists want to predict the long-term trend of global warming. One hypothesis they are considering is that, over very long time spans, the average temperature follows a repeating cycle, but each cycle starts from a higher baseline than the previous one.
Temperatures are measured as five-year averages and expressed as integers in tenths of a degree.
Consider the consecutive differences between neighboring five-year averages. A cycle of length means these differences repeat with period : the first differences form a pattern, and every later difference equals the one positions earlier (the final repetition of the pattern may be truncated).
For example, from the averages
3, 4, 6, 4, 5, 7, 5
the differences are +1, +2, −2, +1, +2, −2. The pattern (+1, +2, −2) of length 3 repeats exactly, so the shortest cycle has length 3.
As another example, from the averages
3, 4, 6, 7
the differences are +1, +2, +1. The pattern (+1, +2) of length 2 repeats, with the second repetition truncated after a single element, so the shortest cycle has length 2.
Given a sequence of temperatures, find the length of the shortest such cycle.
Input
The input consists of several test cases. Each test case begins with an integer (), the number of temperatures, followed by the temperatures themselves. Each temperature is an integer with . Numbers are separated by whitespace.
A line containing a single marks the end of the input and must not be processed.
Output
For each test case, output on its own line the length of the shortest temperature cycle. Such a cycle always exists, because the entire sequence of differences can be treated as one long cycle.