![JAR search and dependency download from the Maven repository](/logo.png)
g2801_2900.s2845_count_of_interesting_subarrays.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2801_2900.s2845_count_of_interesting_subarrays
// #Medium #Array #Hash_Table #Prefix_Sum #2023_12_18_Time_703_ms_(100.00%)_Space_71.8_MB_(100.00%)
class Solution {
fun countInterestingSubarrays(nums: List, modulo: Int, k: Int): Long {
var prefixCnt = 0
val freq: MutableMap = HashMap()
freq[0] = 1
var interestingSubarrays: Long = 0
for (num in nums) {
if (num % modulo == k) {
prefixCnt++
}
val expectedPrefix = (prefixCnt - k + modulo) % modulo
interestingSubarrays += freq.getOrDefault(expectedPrefix, 0).toLong()
freq[prefixCnt % modulo] = freq.getOrDefault(prefixCnt % modulo, 0) + 1
}
return interestingSubarrays
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy