A harbor authority operates n signal beacons anchored along a channel, each blinking at a fixed interval measured in seconds. To schedule a maintenance window that aligns with every beacon's cycle boundary, the harbor master only needs the greatest common divisor of the shortest blink interval and the longest blink interval among all the beacons. Given the blink intervals, compute this value.
Line 1: a single integer n, the number of beacons. Line 2: n space-separated integers, the blink interval (in seconds) of each beacon, in no particular order.
Print a single integer: the greatest common divisor of the smallest value and the largest value among the given intervals.
2 <= n <= 1000 1 <= interval_i <= 1000
Example 1
Input
5 2 5 6 9 10
Expected
2
Explanation
The smallest interval is 2 and the largest is 10; gcd(2, 10) = 2, so the beacons' shared maintenance window is 2 seconds.
Example 2
Input
5 7 5 6 8 3
Expected
1
Explanation
The smallest interval is 3 and the largest is 8; gcd(3, 8) = 1, so the only interval that evenly divides both is 1 second.
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 →