Problem


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
avoidObstacles(inputArray) = 4.

Check out the image below for better understanding:


Input/Output

  • [input] array.integer inputArray

    Non-empty array of positive integers.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    1 ≤ inputArray[i] ≤ 40.

  • [output] integer

    The desired length.



Solution

def avoidObstacles(inputArray):

s = sorted(set(inputArray))

m = max(s)


for size in range(1, m+2):

steps = set([i for i in range(0, m+size+1, size)])

if len(steps.intersection(i)) == 0:

return step


'Programming > Algorithm' 카테고리의 다른 글

[Algorithm] isIPv4Address  (0) 2018.03.02
[Algorithm] arrayMaximalAdjacentDifference  (0) 2018.03.02
[Algorithm] areEquallyStrong  (0) 2018.03.02
[Algorithm] palindromeRearranging  (0) 2018.02.26
[Algorithm] arrayChange  (0) 2018.02.26

+ Recent posts