Premium Only Content
2461. Maximum Sum of Distinct Subarrays With Length K
You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:
The length of the subarray is k, and
All the elements of the subarray are distinct.
Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
Example 2:
Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.
Constraints:
1 <= k <= nums.length <= 105
1 <= nums[i] <= 105
#define ll long long
class Solution {
public:
long long maximumSubarraySum(vector<int>& nums, int k) {
ll sum=0,ans=0;
int start=0,end=0,n=nums.size();
unordered_map<int,int> mp;
while(end<n){
int val = nums[end];
int lastindex = mp.count(val) ? mp[val] : -1;
while(start <= lastindex || end - start + 1>k){
sum -= nums[start];
start++;
}
mp[val] = end;
sum += nums[end];
if(end - start + 1 == k){
ans = max(ans,sum);
}
end++;
}
return ans;
}
};
-
LIVE
FreshandFit
2 hours agoJoe Budden Arrested For Being A Perv! Tesla Cybertruck Explosion
5,072 watching -
2:08:45
Kim Iversen
5 hours agoNew Year, New PSYOP?: The Fort Bragg Connection In The New Years Terror Attacks
35.4K73 -
1:41:18
Glenn Greenwald
5 hours agoTerror Attacks Exploited To Push Unrelated Narratives; Facing Imminent Firing Squad, Liz Cheney Awarded Presidential Medal | SYSTEM UPDATE #381
65.2K71 -
LIVE
Man in America
7 hours ago🔴 LIVE: Terror Attacks or False Flags? IT DOESN'T ADD UP!!!
1,914 watching -
1:02:38
Donald Trump Jr.
8 hours agoNew Year’s Terror, Latest Breaking News with Sebastian Gorka | TRIGGERED Ep.204
142K279 -
59:59
The StoneZONE with Roger Stone
4 hours agoAfter Years of Targeting Trump, FBI and DOJ are Unprepared to Stop Terror Attacks | The StoneZONE
27.5K5 -
1:26:42
Leonardaisfunny
2 hours ago $1.09 earnedH-1b Visas: Infinity Indians
15.6K17 -
1:08:33
Josh Pate's College Football Show
7 hours ago $0.40 earnedPlayoff Reaction Special: Ohio State Owns Oregon | Texas Survives | UGA vs Notre Dame Takeaways
18.8K1 -
58:04
Kimberly Guilfoyle
6 hours agoFBI's Terror Response Failures, Live with Steve Friend & Kyle Seraphin | Ep. 185
95.3K36 -
2:15:01
WeAreChange
7 hours agoMassive Developments In Vegas Investigation! UNREAL DETONATION, Shocking Details Emerge!
102K34