Skip to content

Instantly share code, notes, and snippets.

@soulmachine
Last active December 14, 2015 21:48
Show Gist options
  • Save soulmachine/5153324 to your computer and use it in GitHub Desktop.
Save soulmachine/5153324 to your computer and use it in GitHub Desktop.
/**
* https://www.hackerrank.com/challenges/pairs
*/
object Solution {
def main(args: Array[String]): Unit = {
val (n, k) = readNK(readLine())
val numbers = readNumbers(readLine())
var answer = 0
for (num <- numbers) {
if (numbers.contains(num + k)) {
answer += 1
}
}
println(answer)
}
private def readNK(line: String): (Int, Int) = {
val temp = line.split(" ")
val N = temp(0).toInt
val K = temp(1).toInt
(N, K)
}
private def readNumbers(line: String): Set[Int] = {
val result = collection.mutable.Set.empty[Int]
val nums = line.split(" ")
for (num <- nums) {
result += num.toInt
}
result.toSet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment