LeetCode/kt/count-number-of-bad-pairs.kt
2025-02-09 14:09:34 +01:00

13 lines
363 B
Kotlin

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()
}
}
}