Round 1
Interview Question — Maximum Sum of Unique Subarray
Given an integer array nums and an integer k, write a function to identify the highest possible sum of a subarray within nums, where:
- The subarray must have exactly
kelements. - All elements in the subarray must be unique.
- If no such subarray exists, return
0.
Example 1
Input:
nums = [3, 2, 2, 3, 4, 6, 7, 7, -1]
k = 4
Output:
20Explanation:
The valid subarray with the highest sum is:
[3, 4, 6, 7]Its sum is:
3 + 4 + 6 + 7 = 20Example 2
Input:
nums = [5, 5, 5, 5, 5]
k = 3
Output:
0Explanation:
Every subarray of length 3 contains duplicate elements, so no valid subarray exists.
Additional Example
Input:
nums = [3, 2, 1, 2, 4, 6, 7, 7, -1]
k = 4The solution should consider every contiguous subarray of length 4 and select the one with:
- exactly
4elements - no duplicate elements
- maximum sum
Comments (3)


