Quant Memo
Coding/●●●●●

Best k-window with all-distinct elements

Given an array nums and a length k, find the maximum sum over all windows of exactly k consecutive elements whose elements are all distinct. If no such window exists, return 0.

nums = [1, 5, 4, 2, 9, 9, 9], k = 3
-> 15         # [4, 2, 9]; windows with repeated 9s are disallowed

Aim for O(n)O(n).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions