Created
September 16, 2016 21:26
-
-
Save paulopatto/7a877ca73fa6de192271920b1de47e92 to your computer and use it in GitHub Desktop.
Hacker Rank
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
SELECT country.continent, floor(avg(city.population)) | |
FROM country country | |
INNER JOIN city city ON city.countrycode = country.code | |
GROUP BY country.continent |
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
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