-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTwoPairSum.kt
41 lines (36 loc) · 926 Bytes
/
TwoPairSum.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
package algorithmdesignmanualbook.sorting
import algorithmdesignmanualbook.print
import kotlin.test.assertTrue
/**
* O(nlogn) algorithm for finding whether there exists a pair of elements, one from S1 and one
* from S2, that add up to x
*/
private fun twoPairSum(array1: Array<Int>, array2: Array<Int>, x: Int): Pair<Int, Int>? {
// O(n)
val map: Map<Int, Int> = array1.associateBy { it }
// O(n)
array2.forEach {
val diff = x - it
// O(1)
if (map.containsKey(diff)) {
return Pair(diff, it)
}
}
return null
}
fun main() {
assertTrue {
twoPairSum(
arrayOf(4, 1, 3, 5, 1),
arrayOf(-2, 2, -1, 0, 3),
1
).print() == Pair(3, -2)
}
assertTrue {
twoPairSum(
arrayOf(4, 1, 3, 5, 1),
arrayOf(-2, 2, -1, 0, 3),
5
).print() == Pair(3, 2)
}
}