So this code will count the total number of pairs of numbers whose difference is K. it is naive method and I need to optimize it. suggestions?
test = $stdin.readlines
input = test[0].split(" ")
numbers = test[1].split(" ")
N = input[0]
K = input[1]
count = 0
for i in numbers
current = i.to_i
numbers.shift
for j in numbers
difference = (j.to_i - current).abs
if (difference == K)
count += 1
end
end
end
puts count
Neven useful? If there is another part of the code we can't see, could you remove the parts we don't need? XDnumbers.shift, doesn't it make the loops innacurate? If you have [1,2,3,4], wouldn'titake only 1 and 3 as values?arr = [1,2,3,4,5,6]for i in arrputs iarr.shiftend