Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Max Sum of Distinct Subarrays, Size K

LeetCode #2461Medium
Fixed window + a frequency map gate

Given an integer array and a length k, find the maximum sum among all contiguous subarrays of length k whose elements are all distinct.

Asked atAmazonGoogle
step 1 / 29
1
5
4
2
9
9
9
[0][1][2][3][4][5][6]
Brute force · re-check every window
1given arr, K
2best ← 0
3for i ← 0 to n − K:
4 win = arr[i .. i+K−1]
5 if win has a duplicate: skip
6 best = max(best, sum(win))
7return best
state
  • n7
  • K3

line 1Among all subarrays of length K = 3, find the largest sum — but ONLY windows whose elements are all DISTINCT count.