Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
<div class="_1l1MA">For each number <code>x</code> in <code>nums</code>, create a sorted list <code>indices<sub>x</sub></code> of all indices <code>i</code> such that <code>nums[i] == x</code>.</div>
<div class="_1l1MA">On every <code>indices<sub>x</sub></code>, execute a sliding window technique.</div>
<div class="_1l1MA">For each <code>indices<sub>x</sub></code>, find <code>i, j</code> such that <code>(indices<sub>x</sub>[j] - indices<sub>x</sub>[i]) - (j - i) <= k</code> and <code>j - i + 1</code> is maximized.</div>
<div class="_1l1MA">The answer would be the maximum of <code>j - i + 1</code> for all <code>indices<sub>x</sub></code>.</div>