-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pairs.kt
55 lines (43 loc) · 1.28 KB
/
Pairs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* https://www.hackerrank.com/challenges/pairs/problem?isFullScreen=true */
/* 해시 사용, (자신 - target)이 해시에 있는지 확인 */
import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*
/*
* Complete the 'pairs' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY arr
*/
fun pairs(target: Int, arr: Array<Int>): Int {
val hash: MutableMap<Int, Boolean> = HashMap()
arr.forEach { num ->
hash[num] = true
}
return arr.filter {hash.contains(it - target)}.size
}
fun main(args: Array<String>) {
val first_multiple_input = readLine()!!.trimEnd().split(" ")
val n = first_multiple_input[0].toInt()
val k = first_multiple_input[1].toInt()
val arr = readLine()!!.trimEnd().split(" ").map{ it.toInt() }.toTypedArray()
val result = pairs(k, arr)
println(result)
}