2Brute2Best
🔥0

Maximum Sum of Distinct Subarrays With Length K

Medium·LeetCode #2461 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

You are given an integer array nums and a number k.

Look at every contiguous block of exactly k elements. A block only counts if all k of its values are different (no value appears twice in it).

Return the largest sum among the blocks that count. If no block counts, return 0.

Input

An integer array nums of length n, and an integer k.

Output

A 64-bit integer: the maximum sum of a length-k subarray with all distinct values, or 0 if there is none.

Example 1
Input: nums = [1, 5, 4, 2, 9, 9, 9], k = 3
Output: 15

The size-3 blocks are [1,5,4]=10, [5,4,2]=11, [4,2,9]=15, [2,9,9] and [9,9,9]. The last two repeat 9, so they don't count. The best of the rest is 15.

Example 2
Input: nums = [4, 4, 4], k = 3
Output: 0

The only block, [4,4,4], has repeated values, so nothing counts and the answer is 0.

Constraints
  • • 1 ≤ k ≤ nums.length ≤ 10^5
  • • 1 ≤ nums[i] ≤ 10^5
  • • A window sum can reach 10^10, which does not fit in a 32-bit int.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“subarrays with length k”
→ Fixed window of size k: one add and one remove per step.
“all elements distinct”
→ Need to know about repeats inside the window → frequency map + distinct counter.
“maximum sum … return 0 if none”
→ Slide the sum too; only qualifying windows update best, which starts at 0.
03

Approach Complexity Ladder

Brute Force — check every window from scratchO(N · K)O(K)

For each window, use a fresh set to test distinctness and sum it

Optimal — fixed window + frequency mapO(N)O(K)

Slide sum and value counts together; a full window counts when distinct == k

Brute Force Intuition

For every start index, walk through the k elements of that window with a fresh hash set. If a value is already in the set, the window has a repeat and doesn't count. Otherwise add up the values and compare with the best.

Recognition rule for this sub-pattern:

Window size is fixed at K. Each slide adds one element on the right and removes one on the left — update the aggregate in O(1).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.