-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathavoidObstacles.R
executable file
·38 lines (38 loc) · 1.11 KB
/
avoidObstacles.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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
#
# [execution time limit] 5 seconds (r)
#
# [input] array.integer inputArray
#
# Non-empty array of positive integers.
#
# Guaranteed constraints:
# 2 ≤ inputArray.length ≤ 1000,
# 1 ≤ inputArray[i] ≤ 1000.
#
# [output] integer
# The desired length.
# inputArray <- list(1, 4, 10, 6, 2)
avoidObstacles <- function(inputArray) {
inputArray <- unlist(inputArray)
inputArray <- inputArray[order(inputArray)]
for (ind in 2:(max(inputArray) + 1)) {
if (sum(inputArray %% ind == 0) == 0) {
return(ind)
}
}
}
#to think through: maxindex = max + 1 ? never more? ya makes sense. hmm.