3026. Maximum Good Subarray Sum LeetCode Solution

In this guide, you will get 3026. Maximum Good Subarray Sum LeetCode Solution with the best time and space complexity. The solution to Maximum Good Subarray Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Good Subarray Sum solution in C++
  4. Maximum Good Subarray Sum solution in Java
  5. Maximum Good Subarray Sum solution in Python
  6. Additional Resources
3026. Maximum Good Subarray Sum LeetCode Solution image

Problem Statement of Maximum Good Subarray Sum

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 exactly k, in other words, the subarray nums[i..j] is good if |nums[i] – nums[j]| == k.
Return the maximum sum of a good subarray of nums. If there are no good subarrays, return 0.

Example 1:

Input: nums = [1,2,3,4,5,6], k = 1
Output: 11
Explanation: The absolute difference between the first and last element must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].

Example 2:

Input: nums = [-1,3,2,4,5], k = 3
Output: 11
Explanation: The absolute difference between the first and last element must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].

See also  1929. Concatenation of Array LeetCode Solution

Example 3:

Input: nums = [-1,-2,-3,-4], k = 2
Output: -6
Explanation: The absolute difference between the first and last element must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].

Constraints:

2 <= nums.length <= 105
-109 <= nums[i] <= 109
1 <= k <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3026. Maximum Good Subarray Sum LeetCode Solution in C++

class Solution {
 public:
  long long maximumSubarraySum(vector<int>& nums, int k) {
    long ans = LONG_MIN;
    long prefix = 0;
    // {num: the minimum prefix sum excluding `num`}
    unordered_map<int, long> numToMinPrefix;

    for (const int num : nums) {
      if (const auto it = numToMinPrefix.find(num);
          it == numToMinPrefix.cend() || it->second > prefix) {
        numToMinPrefix[num] = prefix;
      }
      prefix += num;
      if (const auto it = numToMinPrefix.find(num + k);
          it != numToMinPrefix.cend())
        ans = max(ans, prefix - it->second);
      if (const auto it = numToMinPrefix.find(num - k);
          it != numToMinPrefix.cend())
        ans = max(ans, prefix - it->second);
    }

    return ans == LONG_MIN ? 0 : ans;
  }
};
/* code provided by PROGIEZ */

3026. Maximum Good Subarray Sum LeetCode Solution in Java

class Solution {
  public long maximumSubarraySum(int[] nums, int k) {
    long ans = Long.MIN_VALUE;
    long prefix = 0;
    Map<Integer, Long> numToMinPrefix = new HashMap<>();

    for (final int num : nums) {
      if (!numToMinPrefix.containsKey(num) || numToMinPrefix.get(num) > prefix)
        numToMinPrefix.put(num, prefix);
      prefix += num;
      if (numToMinPrefix.containsKey(num + k))
        ans = Math.max(ans, prefix - numToMinPrefix.get(num + k));
      if (numToMinPrefix.containsKey(num - k))
        ans = Math.max(ans, prefix - numToMinPrefix.get(num - k));
    }

    return ans == Long.MIN_VALUE ? 0 : ans;
  }
}
// code provided by PROGIEZ

3026. Maximum Good Subarray Sum LeetCode Solution in Python

class Solution:
  def maximumSubarraySum(self, nums: list[int], k: int) -> int:
    ans = -math.inf
    prefix = 0
    numToMinPrefix = {}

    for num in nums:
      if num not in numToMinPrefix or numToMinPrefix[num] > prefix:
        numToMinPrefix[num] = prefix
      prefix += num
      if num + k in numToMinPrefix:
        ans = max(ans, prefix - numToMinPrefix[num + k])
      if num - k in numToMinPrefix:
        ans = max(ans, prefix - numToMinPrefix[num - k])

    return 0 if ans == -math.inf else ans
# code by PROGIEZ

Additional Resources

See also  2135. Count Words Obtained After Adding a Letter LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.