# Enter your code here. Read input from STDIN. Print output to STDOUT
n = gets.chomp.to_i
vals = gets.chomp.split(" ").map(&:to_i)
min_diff = (1.0)/(0.0)
for i in 1...(vals.length)
# compare all nums that come before val at index i
for j in (i-1).downto(0)
computed_diff = (vals[j] - vals[i])
min_diff = computed_diff if computed_diff > 0 && computed_diff < min_diff
end
end
puts min_diff
TIMEOUT for some of the solutions
Would sorting the already compared ints in array save run time?
Ex. 20 7 8 5 2
7 compared to 20
8 compared to 20,7
5 compared to 20,7,8 if sorted 5 compared to 20,8,7 stop as soon as num is > 5
No comments:
Post a Comment