You are given an array nums of length n and a positive integer k.
A subarray of nums is called good if the absolute difference between its first and last element is exactlyk, in other words, the subarray
nums[i..j] is good if |nums[i] - nums[j]| == k.
Return _themaximum sum of a good subarray of _nums. If there are no good subarrays, return0.
Input: nums =[1,2,3,4,5,6], k =1Output: 11Explanation: The absolute difference between the first and last element must be 1for a good subarray. All the good subarrays are:[1,2],[2,3],[3,4],[4,5], and [5,6]. The maximum subarray sum is11for the subarray [5,6].
Input: nums =[-1,3,2,4,5], k =3Output: 11Explanation: The absolute difference between the first and last element must be 3for a good subarray. All the good subarrays are:[-1,3,2], and [2,4,5]. The maximum subarray sum is11for the subarray [2,4,5].
Input: nums =[-1,-2,-3,-4], k =2Output: -6Explanation: The absolute difference between the first and last element must be 2for a good subarray. All the good subarrays are:[-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is-6for the subarray [-1,-2,-3].
We want to find the maximum sum of a subarray whose first and last elements differ by exactly k. For each index, we can use prefix sums and a hash map to track the best prefix sum for each possible value of nums[i] ± k. This allows us to efficiently check for all possible good subarrays ending at each index.
classSolution {
public:longlong maximumSubarraySum(vector<int>& nums, int k) {
unordered_map<int, longlong> mp;
longlong ans = LLONG_MIN, sum =0;
mp[nums[0]] =0;
for (int i =0; i < nums.size(); ++i) {
sum += nums[i];
if (mp.count(nums[i] + k)) ans = max(ans, sum - mp[nums[i] + k]);
if (mp.count(nums[i] - k)) ans = max(ans, sum - mp[nums[i] - k]);
if (!mp.count(nums[i])) mp[nums[i]] = sum;
}
return ans == LLONG_MIN ?0: ans;
}
};
funcmaximumSubarraySum(nums []int, kint) int64 {
mp:=map[int]int64{}
varansint64 = -1<<63varsumint64 = 0mp[nums[0]] = 0fori:=0; i < len(nums); i++ {
sum+= int64(nums[i])
ifv, ok:=mp[nums[i]+k]; ok {
ifsum-v > ans {
ans = sum-v }
}
ifv, ok:=mp[nums[i]-k]; ok {
ifsum-v > ans {
ans = sum-v }
}
if_, ok:=mp[nums[i]]; !ok {
mp[nums[i]] = sum }
}
ifans==-1<<63 {
return0 }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publiclongmaximumSubarraySum(int[] nums, int k) {
Map<Integer, Long> mp =new HashMap<>();
long ans = Long.MIN_VALUE, sum = 0;
mp.put(nums[0], 0L);
for (int i = 0; i < nums.length; ++i) {
sum += nums[i];
if (mp.containsKey(nums[i]+ k)) ans = Math.max(ans, sum - mp.get(nums[i]+ k));
if (mp.containsKey(nums[i]- k)) ans = Math.max(ans, sum - mp.get(nums[i]- k));
mp.putIfAbsent(nums[i], sum);
}
return ans == Long.MIN_VALUE? 0 : ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funmaximumSubarraySum(nums: IntArray, k: Int): Long {
val mp = mutableMapOf<Int, Long>()
var ans = Long.MIN_VALUE
var sum = 0L mp[nums[0]] = 0Lfor (i in nums.indices) {
sum += nums[i]
mp[nums[i]+k]?.let { ans = maxOf(ans, sum - it) }
mp[nums[i]-k]?.let { ans = maxOf(ans, sum - it) }
mp.putIfAbsent(nums[i], sum)
}
returnif (ans ==Long.MIN_VALUE) 0Lelse ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defmaximumSubarraySum(self, nums: list[int], k: int) -> int:
mp: dict[int, int] = {nums[0]: 0}
ans = float('-inf')
s =0for x in nums:
s += x
if x + k in mp:
ans = max(ans, s - mp[x + k])
if x - k in mp:
ans = max(ans, s - mp[x - k])
if x notin mp:
mp[x] = s
return0if ans == float('-inf') else ans