All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2364_count_number_of_bad_pairs.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2301_2400.s2364_count_number_of_bad_pairs

// #Medium #Array #Hash_Table #2023_07_02_Time_590_ms_(100.00%)_Space_54.8_MB_(100.00%)

class Solution {
    fun countBadPairs(nums: IntArray): Long {
        val seen = HashMap()
        var count: Long = 0
        for (i in nums.indices) {
            val diff = i - nums[i]
            count += if (seen.containsKey(diff)) {
                (i - seen[diff]!!).toLong()
            } else {
                i.toLong()
            }
            seen[diff] = seen.getOrDefault(diff, 0) + 1
        }
        return count
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy