Last active
June 29, 2017 04:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ankuriitk Please find the solution, I'm not that richer by Ruby. Learned little more.... (.inspect and .to_s)