Loading...
You are given an integer array nums and an integer k. Consider every contiguous window (subarray) of nums that has length exactly k. A window qualifies when all k of its elements are distinct (no value repeats inside the window).
Return the maximum sum among all qualifying windows. If no window of length k has all-distinct elements, return 0.
A subarray is a contiguous, non-empty sequence of elements within the array.
Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The length-3 windows are:
- [1,5,4] has distinct elements and sum 10.
- [5,4,2] has distinct elements and sum 11.
- [4,2,9] has distinct elements and sum 15.
- [2,9,9] does not qualify because 9 repeats.
- [9,9,9] does not qualify because 9 repeats.
The maximum sum among qualifying windows is 15.
Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The only length-3 window is [4,4,4], which does not qualify because 4 repeats. No window qualifies, so the answer is 0.
k ≤ nums.length ≤105nums[i] ≤105Click "Run" to test with sample cases or "Submit" to run all tests.