BugForge
DSA

Best K-Day Window With Distinct Products

Mediumsliding windowhash map 00:00

sales[i] is the product id sold on day i (its value is also its revenue). Among all windows of exactly k consecutive days where all values are distinct, return the maximum sum. Return 0 if none exist.

Signature

maxDistinctWindow(sales: int[], k: int) → int
Example 1
sales = [1,5,4,2,9,9,9]
k = 3
output = 15
[5,4,2]=11, [4,2,9]=15 ← best; windows with repeated 9s don't count
Example 2
sales = [4,4,4]
k = 3
output = 0
no window has distinct values → 0

Constraints

  • 1 ≤ k ≤ n ≤ 10^5
  • 1 ≤ sales[i] ≤ 10^5
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).