kt: add «2364. Count Number of Bad Pairs»

URL:	https://leetcode.com/problems/count-number-of-bad-pairs/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-02-09 14:09:34 +01:00
parent f15c12a030
commit 788199789d
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,13 @@
class Solution {
fun countBadPairs(nums: IntArray): Long =
mutableMapOf<Int, Int>().let { counters ->
nums.indices.sumOf { i ->
val diff = i - nums[i]
val goodPairs = counters.get(diff) ?: 0
counters[diff] = goodPairs + 1
(i - goodPairs).toLong()
}
}
}