#3837
Medium Algorithms Delayed count of equal elements
Array Hash Table Counting
80.6% acceptance
Apr 3, 2026
5
2
You are given an integer array nums of length n and an integer k.
For each index i, define the delayed count as the number of indices j such that:
i + k < j <= n - 1, and
nums[j] == nums[i]
Return an array ans where ans[i] is the delayed count of index i.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn delayed_count(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let gap = k as usize;
let mut freq = vec![0i32; 100_001];
let mut answer = vec![0; n];
for index in (0..n).rev() {
let delayed_index = index + gap + 1;
if delayed_index < n {
freq[nums[delayed_index] as usize] += 1;
}
answer[index] = freq[nums[index] as usize];
}
answer
}
}