Minimum Subarray Length With Distinct Sum At Least K
You are given an integer array nums and an integer k.
Find the minimum length of a contiguous subarray where the sum of its distinct values is greater than or equal to k. When calculating this sum, each unique number in the subarray is added to the total exactly once, regardless of how many times it repeats.
If there is no such subarray that meets or exceeds the sum k, return -1.
Example 1:
Input: nums = [1, 2, 3], k = 5
Output: 2
Explanation:
The subarray [2, 3] contains distinct elements {2, 3} whose sum is 2 + 3 = 5. This meets k = 5, and the length is 2.
Example 2:
Input: nums = [5, 5, 5, 5], k = 6
Output: -1
Explanation: Because all elements are the same, any subarray will only have a distinct sum of 5. It is impossible to reach or exceed k = 6.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [1, 1, 1, 1], k = 2
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 1 <= nums[i] <= 105
- 1 <= k <= 109