Skip to content

Practice Maximum Sum of Almost Unique Subarray

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2841
LeetCode Problem

Maximum Sum of Almost Unique Subarray

Problem Statement

You are given an integer array nums and two positive integers m and k. Return the maximum sum out of all almost unique subarrays of length k of nums. If no such subarray exists, return 0. A subarray of nums is almost unique if it contains at least m distinct elements. A subarray is a contiguous non-empty sequence of elements within an array.

Source: Maximum Sum of Almost Unique Subarray on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,6,7,3,1,7], m = 3, k = 4
Output
18

# Example 2

Input
nums = [5,9,9,2,4,5,4], m = 1, k = 3
Output
23

# Example 3

Input
nums = [1,2,1,2,1,2,1], m = 3, k = 3
Output
0

Constraints

1 <= nums.length <= 2 * 10^4
1 <= m <= k <= nums.length
1 <= nums[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Maximum Sum of Almost Unique Subarray.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.