Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Which elements change when moving from the subarray of size k that ends at index i to the subarray of size k that ends at index i + 1?
Only two elements change, the element at i + 1 is added into the subarray, and the element at i - k + 1 gets removed from the subarray.
Iterate through each subarray of size k and keep track of the sum of the subarray and the frequency of each element.