Explorer
Data Structures & Algorithms

Return the actual subarray elements that produce the maximum subarray sum.

Problem Statement

Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return the subarray itself. If multiple subarrays have the maximum sum, return any one of them.

Examples

Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

Output: [4, -1, 2, 1]

Explanation: Combining the input according to Print subarray with maximum subarray sum (extended version of above problem) logic yields [4, -1, 2, 1].

Input: nums = [1]

Output: [1]

Explanation: Combining the input according to Print subarray with maximum subarray sum (extended version of above problem) logic yields [1].

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

💡 Hint 1: Extend Kadane’s algorithm by tracking start and end indices of the maximal subarray. 💡 Hint 2: Whenever currentSum becomes 0 (or resets), set a temporary start index to i. 💡 Hint 3: Whenever currentSum exceeds maxSum, record ansStart = start and ansEnd = i.

Editorial & Approach

Problem Overview & Intuition

By maintaining start and end pointers alongside the Kadane accumulation, whenever a new maximum sum is discovered, we update the boundary range [ansStart, ansEnd]. At the end, we slice and return the subarray.

Step-by-Step Approach

  1. Track maxSum = -Infinity, currentSum = 0, start = 0, ansStart = 0, ansEnd = 0.
  2. For each index i:
  3. If currentSum === 0, mark start = i.
  4. Add nums[i] to currentSum.
  5. If currentSum > maxSum, update maxSum = currentSum, ansStart = start, ansEnd = i.
  6. If currentSum < 0, reset currentSum = 0.
  7. Return nums.slice(ansStart, ansEnd + 1).

Optimal Implementation (JavaScript)

function maxSubarrayElements(nums) {
  let maxSum = -Infinity;
  let currentSum = 0;
  let start = 0, ansStart = 0, ansEnd = 0;

  for (let i = 0; i < nums.length; i++) {
    if (currentSum === 0) start = i;
    currentSum += nums[i];

    if (currentSum > maxSum) {
      maxSum = currentSum;
      ansStart = start;
      ansEnd = i;
    }

    if (currentSum < 0) {
      currentSum = 0;
    }
  }

  return nums.slice(ansStart, ansEnd + 1);
}

Complexity Analysis

Time Complexity O(N) — single pass to find indices and O(K) slice.
Space Complexity O(1) auxiliary space (excluding returned subarray).

Edge Cases & Corner Traps Handled

  • All negative elements: returns the single least negative element.
  • All positive elements: returns the entire array.
  • Single element array: returns the array itself.

Print subarray with maximum subarray sum (extended version of above problem)

Medium
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return the subarray itself. If multiple subarrays have the maximum sum, return any one of them.
Example Scenarios
1Example 1
Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: [4, -1, 2, 1]
Explanation:

Combining the input according to Print subarray with maximum subarray sum (extended version of above problem) logic yields [4, -1, 2, 1].

2Example 2
Input: nums = [1]
Output: [1]
Explanation:

Combining the input according to Print subarray with maximum subarray sum (extended version of above problem) logic yields [1].

Editor
Loading Editor...
nums =
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output:Click "Run" above to execute and verify your code here.
[4,-1,2,1]