Hackerrank – Pairs

Problem Statement
A description of the problem can be found on Hackerrank.

Solution
Sort the array. Choose one element and check continously every next element after chosen element if their difference is equals to k. If it is greater stop comparisons and choose next element.

Note: JavaScript and Ruby solutions use the same algorithm as other solutions, but some Hackerrank tests failed on timeout.

I created solution in:

All solutions are also available on my GitHub.

Java

JavaScript

Ruby

Scala