# Untitled

By: a guest on Aug 19th, 2012  |  syntax: None  |  size: 1.01 KB  |  hits: 9  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. irb(main):001:0> def pairs_with_k_difference
2. irb(main):002:1>   require 'set'
3. irb(main):003:1>   n, k = \$stdin.gets.match(/(\d+) (\d+)/).captures
4. irb(main):004:1>   if (n.is_a?Integer and k.is_a?Integer and n <= 10^5 and K>0 && K<1e9)
5. irb(main):005:2>     # n = n.to_i
6. irb(main):006:2*     # k = k.to_i
7. irb(main):007:2*     flag = 0
8. irb(main):008:2>     numbers = Set.new(\$stdin.gets.split)
9. irb(main):009:2>     numbers.each do|a|
10. irb(main):010:3*       if !a.is_a? Integer
11. irb(main):011:4>       flag = flag + 1
12. irb(main):012:4>       end
13. irb(main):013:3>     end
14. irb(main):014:2>     if flag == 0
15. irb(main):015:3>       puts numbers.count { |num| numbers.include?(num-k) }
16. irb(main):016:3>     else
17. irb(main):017:3*       \$stdin.puts "Please enter numbers"
18. irb(main):018:3>     end
19. irb(main):019:2>   else
20. irb(main):020:2*     \$stdout.puts "Please enter values within limits"
21. irb(main):021:2>   end
22. irb(main):022:1> end
23. => nil
24. irb(main):023:0> pairs_with_k_difference
25. 5 2
26. Please enter values within limits
27. => nil
28. irb(main):024:0>