BackmediumArraysTCSRazorpay

Maximum Subarray Sum with Single Element Doubling Solution

Problem Statement

Given an integer array nums, you must choose exactly one element and double its value. After performing this operation, find the maximum possible sum of a non-empty subarray.

Example 1
Input
[1, 3, -2]
Output
5

Explanation: Step-by-step: with input [1, 3, -2], we double the element at index 0 (value 1) to get [2, 3, -2]. The maximum possible sum of a non-empty subarray after doubling one element is 2 + 3 = 5.

Example 2
Input
[2, -1, 4, 8]
Output
21

Explanation: Step-by-step: with input [2, -1, 4, 8], we double the element at index 3 (value 8) to get [2, -1, 4, 16]. The maximum possible sum of a non-empty subarray after doubling one element is 2 + (-1) + 4 + 16 = 21.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximum Subarray Sum with Single Element Doubling — Problem Statement & Solution Guide

ArraysMediumKadane's Algorithm
TimeO(n)
|
SpaceO(1)

Problem Description

Given an integer array nums, you must choose exactly one element and double its value. After performing this operation, find the maximum possible sum of a non-empty subarray.

Examples

Example 1

Input

[1, 3, -2]

Output

5

Explanation: Step-by-step: with input [1, 3, -2], we double the element at index 0 (value 1) to get [2, 3, -2]. The maximum possible sum of a non-empty subarray after doubling one element is 2 + 3 = 5.

Example 2

Input

[2, -1, 4, 8]

Output

21

Explanation: Step-by-step: with input [2, -1, 4, 8], we double the element at index 3 (value 8) to get [2, -1, 4, 16]. The maximum possible sum of a non-empty subarray after doubling one element is 2 + (-1) + 4 + 16 = 21.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Optimal Approach & Strategy

Use dynamic programming to track two states: the max subarray ending at i without a doubled element and the max subarray ending at i with exactly one doubled element. This single-pass scan provides the answer in linear time.

Brute Force Approach

Iterate through every index of the array, create a copy where that element is doubled, and run Kadane's algorithm to find the maximum subarray sum. This approach checks every possible doubling scenario individually.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maximumSubarraySumWithSingleElementDoubling(nums) {
  let maxVal = -Infinity;
  for (let i = 0; i < nums.length; i++) {
    let dp0 = nums[i];
    let dp1 = nums[i] * 2;
    maxVal = Math.max(maxVal, dp0, dp1);
    for (let j = i + 1; j < nums.length; j++) {
      dp0 = Math.max(nums[j], dp0 + nums[j]);
      dp1 = Math.max(dp1, Math.max(dp0 + nums[j], dp0 + nums[j] * 2));
      maxVal = Math.max(maxVal, dp0, dp1);
    }
  }
  return maxVal;
}

Asked in Top Tech Interviews

TCSRazorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.