Last active
December 14, 2015 21:48
-
-
Save soulmachine/5153324 to your computer and use it in GitHub Desktop.
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
/** | |
* 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