Advertisement

Maximum Good Subarray Sum - LeetCode 3026 Solution

Maximum Good Subarray Sum - Complete Solution Guide

Maximum Good Subarray Sum is LeetCode problem 3026, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 goo

Detailed Explanation

The problem asks us to find the maximum sum of a 'good' subarray within a given array `nums`. A subarray is considered 'good' if the absolute difference between its first and last elements is exactly equal to a given integer `k`. The goal is to iterate through all possible subarrays, check if they are 'good', calculate their sum, and return the maximum sum among all 'good' subarrays. If no such subarray exists, the function should return 0.

Solution Approach

The solution uses a prefix sum approach along with a hash map to efficiently find 'good' subarrays. It calculates the prefix sum as it iterates through the array. For each number, it checks if there's a previous number (using the hash map) such that their absolute difference is equal to `k`. If such a number exists, the sum of the corresponding subarray is calculated using the prefix sums and compared to the current maximum sum. The hash map stores each number encountered along with its minimum prefix sum so far. This way, when a good subarray is detected, the difference in prefix sums is maximized, leading to a potentially greater subarray sum.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_s` (maximum sum) to negative infinity and `found` (boolean to indicate if a good subarray was found) to `false`.
  2. Step 2: Initialize `prefix_sum` to 0 and `min_prefix_map` (hash map) to store each number's minimum prefix sum.
  3. Step 3: Iterate through the `nums` array.
  4. Step 4: For each `num` in `nums`, calculate `target1 = num - k` and `target2 = num + k`.
  5. Step 5: Check if `target1` exists in `min_prefix_map`. If it does, calculate the current subarray sum as `prefix_sum + num - min_prefix_map[target1]`. Update `max_s` with the maximum of `max_s` and the `current_sum`. Set `found` to `true`.
  6. Step 6: Repeat Step 5 for `target2`.
  7. Step 7: If `num` is not in `min_prefix_map` or if the current `prefix_sum` is less than the existing value in `min_prefix_map[num]`, update `min_prefix_map[num]` with the current `prefix_sum`. This ensures that we always store the minimum prefix sum for each number.
  8. Step 8: Update `prefix_sum` by adding `num` to it.
  9. Step 9: After iterating through all numbers, return `max_s` if `found` is true, otherwise return 0.

Key Insights

  • Insight 1: Using a prefix sum to calculate subarray sums efficiently can significantly reduce the time complexity.
  • Insight 2: Employing a hash map (or dictionary) to store and quickly retrieve previously encountered prefix sums allows for an O(1) lookup time when checking for potential 'good' subarrays.
  • Insight 3: Keeping track of the minimum prefix sum for each number encountered helps to maximize the overall sum when a 'good' subarray is found. This addresses the possibility of negative numbers within the subarrays.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Prefix Sum.

Companies

Asked at: Atlassian, Groww, Zepto.