new_arr=arr.each_cons(2).to_a

tmp = 0
result=[]
new_arr.each do |a|

  if tmp == 0
    if ((tmp-a[0]).abs)<=k
      result << a
      tmp = 0
    else
      tmp = a[0]
    end
  end
  if ((a[1]-a[0]).abs)<=k
    result << a
  else
    tmp = a[0]
  end
end

p result.length