Skip to content

Instantly share code, notes, and snippets.

@paulopatto
Created September 16, 2016 21:26
Show Gist options
  • Save paulopatto/7a877ca73fa6de192271920b1de47e92 to your computer and use it in GitHub Desktop.
Save paulopatto/7a877ca73fa6de192271920b1de47e92 to your computer and use it in GitHub Desktop.
Hacker Rank
SELECT country.continent, floor(avg(city.population))
FROM country country
INNER JOIN city city ON city.countrycode = country.code
GROUP BY country.continent
def pairs(a,k)
pairs_with_tail_recursion( a.sort, k, 0)
end
def pairs_with_tail_recursion(list, diff, acm)
return acm if list.empty?
element = list[0]
sublist = list[1..-1]
acm += sublist.select { |n| n == element - diff || n == element + diff }.count
pairs_with_tail_recursion(sublist, diff, acm)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment