kt: add «1726. Tuple with Same Product»

URL:	https://leetcode.com/problems/tuple-with-same-product/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-02-06 15:17:44 +01:00
parent e3be56387e
commit e6e431d528
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,16 @@
class Solution {
fun tupleSameProduct(nums: IntArray): Int =
buildMap<Int, Int> {
nums.indices
.flatMap { i ->
nums.indices.drop(i + 1).map { j ->
nums[i] to nums[j]
}
}.forEach { (x, y) ->
put(x * y, 1 + (get(x * y) ?: 0))
}
}.entries.sumOf { (p, count) ->
val pairs = (count - 1) * count / 2
8 * pairs
}
}