Skip to content

Instantly share code, notes, and snippets.

@shailesh
Last active June 29, 2017 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shailesh/085523d678dfcf98bfd916dffff5cbe5 to your computer and use it in GitHub Desktop.
Save shailesh/085523d678dfcf98bfd916dffff5cbe5 to your computer and use it in GitHub Desktop.
This will print three elements in an array whose sum is equal to an existing array
arr = [1, 2, 4, 10, 90, 302, 312, 500]
(2..arr.count).each do |len|
arr.combination(len).each do |comb|
sum = comb.inject(:+)
if arr.include? sum
puts (comb << sum).inspect
end
end
end
@shailesh
Copy link
Author

@ankuriitk Please find the solution, I'm not that richer by Ruby. Learned little more.... (.inspect and .to_s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment