You are given an array of integers representing coordinates of obstacles situated on a straight line.

Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.

Find the minimal length of the jump enough to avoid all the obstacles.

Example

For inputArray = [5, 3, 6, 7, 9], the output should be

solution(inputArray) = 4.

Check out the image below for better understanding:

Input/Output

풀이

int solution(int[] inputArray) {
    // 배열안의 숫자가 n 으로 완벽히 나누어지면 안된다.
    // 그리고 점프하는 수는 최소의 수가 되어야 한다.
    int n = 2;
    // 재도전 유무
    boolean retry = false;
    
    while (true) {
        // 시작은 재도전 x
        retry = false;
        
        for (int i : inputArray) {
            // 나누어떨어지면 재도전
            if (i % n == 0) {
                retry = true;
                break;
            }
        }
        
        // 재도전이면 숫자 증가
        if (retry) n ++;
        else return n;
    }
}