Skip to content

Instantly share code, notes, and snippets.

@mashiro
Last active October 3, 2020 17:12
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 mashiro/f19c9053c0a17055275380fa9c656a8e to your computer and use it in GitHub Desktop.
Save mashiro/f19c9053c0a17055275380fa9c656a8e to your computer and use it in GitHub Desktop.
K-diff Pairs in an Array
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def find_pairs(nums, k)
if k == 0
return nums.group_by { |n| n }
.map { |k, v| [k, v.size] }
.filter { |(n, c)| c >= 2 }
.size
end
a = Set.new nums
a.inject(0) { |m, n| a.include?(n + k) ? m + 1 : m }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment