I am attempting a coding problem which asks me to print "YES" if the sum of any consecutive array numbers is equal to the given number. The link to the challenge is here
my solution is here
b = Array.new
a = Array.new
t = gets.to_i
if t >= 0 && t <= 10
t.times do
n, x = gets.chomp.split.map(&:to_i)
n.times do
a << gets.to_i
end
(1..a.length).each do |num|
a.each_cons(num).each do |pair|
if pair.inject(:+) == x
b << "YES"
else
b << "NO"
end
end
end
if b.include?("YES")
puts "YES"
else
puts "NO"
end
end
end
Although they have accepted my answer, it does not pass all the test cases, hence I am not satisfied.Can someone help me with a correct, more efficient and elegant solution?
Aucun commentaire:
Enregistrer un commentaire