Skip to main content

Maximum Subarray Sum After Multiplier - Solution & Explanation

MediumArrayDynamic Programming11 min read
Practice this problem

Problem Statement

You are given an integer array nums and a positive integer k.

You must choose exactly one subarray of nums and perform exactly one of the following operations:

  1. Multiply each number in the chosen subarray by k.
  2. Divide each number in the chosen subarray by k.
    • When dividing a positive number by k, use the floor value of the division result.
    • When dividing a negative number by k, use the ceiling value of the division result.

Return the maximum possible sum of a non-empty subarray in the resulting array.

Note that the subarray chosen for the operation and the subarray chosen for the sum may be different.

 

Example 1:

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

Output: 14

Explanation:

  • Multiply each number in the subarray [3, 4] by 2.
  • This results in nums = [1, -2, 6, 8, -5].
  • The subarray with the largest sum is [6, 8], so the output is 6 + 8 = 14.

Example 2:

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

Output: -1

Explanation:

  • Divide each number in the subarray [-3] by 2.
  • This results in nums = [-5, -4, -1].
  • The subarray with the largest sum is [-1], so the output is -1.

 

Constraints:

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

Approach Overview

Problem Overview: You are given an array and a multiplier. You may choose one contiguous subarray and multiply every element in that subarray by the multiplier exactly once. After the operation, compute the maximum possible subarray sum. The challenge is deciding where applying the multiplier increases the best possible sum.

Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)

Try every possible subarray [l, r] as the segment to multiply. For each choice, create the modified array where elements in [l, r] are multiplied by the factor. Run a standard maximum subarray calculation such as Kadane’s algorithm to compute the best subarray sum of the modified array. There are O(n^2) choices for [l, r], and each evaluation takes O(n), giving O(n^3) time. This method is straightforward but impractical for large inputs.

Approach 2: Prefix-Based Simulation (O(n^2) time, O(n) space)

Instead of rebuilding the array each time, compute prefix sums and simulate the effect of multiplying a candidate subarray. Fix the start index l, extend the end index r, and dynamically track how the multiplied segment affects the total. For each pair (l, r), combine the multiplied segment contribution with the best prefix and suffix subarray sums. This reduces redundant recomputation but still requires checking O(n^2) candidate segments. Useful for reasoning about the structure of the optimal solution before implementing a linear-time method.

Approach 3: Dynamic Programming with Kadane States (O(n) time, O(1) space)

The optimal solution tracks three running states while iterating through the array. State 1: the maximum subarray sum ending at index i without using the multiplier. State 2: the maximum subarray sum where the multiplier is currently being applied. State 3: the maximum subarray sum where the multiplier has already been used and the subarray continues normally. Each step updates these states using transitions similar to Kadane's algorithm. For example, when entering the multiplied segment, multiply the current element before adding it to the running sum. This state-machine view ensures the multiplier is applied to exactly one contiguous segment while maintaining linear time.

The key insight: the optimal solution can be expressed as transitions between three phases—before multiplication, during multiplication, and after multiplication. Maintaining these states while scanning once avoids explicitly enumerating subarrays. This pattern is common in dynamic programming problems involving a single modification to a subarray and builds directly on techniques used for array optimization problems.

Recommended for interviews: The linear dynamic programming approach with Kadane-style state transitions is the expected solution. Interviewers like it because it demonstrates that you can convert a brute-force subarray modification problem into a constant-state DP scan. Mentioning the brute force approach first shows understanding of the search space, but implementing the O(n) DP proves algorithmic maturity.

Solution

We define f[i][j] as the maximum subarray sum ending at nums[i] with current state j. There are 4 states for j:

  • State 0: the current subarray has not undergone any operation yet;
  • State 1: the current subarray is being multiplied by k;
  • State 2: the current subarray is being divided by k;
  • State 3: the operation on the current subarray has been completed.

Initially, f[0][0] = 0, and all other f[i][j] = -infty.

Next, we consider the state transitions. For the i-th number nums[i], we can choose not to perform any operation, multiply by k, divide by k, or continue after the operation has been completed:

  • If we perform no operation, then f[i][0] = max(f[i-1][0], 0) + nums[i];
  • If we multiply by k, then f[i][1] = max(f[i-1][0], f[i-1][1], 0) + nums[i] times k;
  • If we divide by k, then f[i][2] = max(f[i-1][0], f[i-1][2], 0) + \lfloor \frac{nums[i]}{k} \rfloor;
  • If the operation has been completed, then f[i][3] = max(f[i-1][1], f[i-1][2], f[i-1][3]) + nums[i].

We take the maximum among all states as the answer.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^3)O(1)Small arrays or for verifying correctness during early problem exploration
Prefix-Based SimulationO(n^2)O(n)When optimizing brute force and reasoning about subarray contributions
Dynamic Programming (Kadane States)O(n)O(1)General case and expected interview solution for large inputs

Video Solution

Leetcode Weekly Contest 508 | Q3 - Maximum Subarray Sum After Multiplier | 3976DSA with Kumar K1,427 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Maximum Subarray Sum After Multiplier easy or hard?
The problem is typically rated Medium. Understanding Kadane’s algorithm is not enough by itself; you must extend it with multiple DP states to model the phase where the multiplier is applied.
Maximum Subarray Sum After Multiplier Python/Java solution
Python and Java implementations both follow the same DP idea: maintain three variables for the states and update them for each element. The algorithm performs constant-time transitions per element, leading to O(n) time and O(1) space in both languages.
How to solve Maximum Subarray Sum After Multiplier in O(n)?
Maintain three running values during a single pass: the best subarray sum without using the multiplier, the best sum while the multiplier is currently active, and the best sum after it has already been applied. Update them using Kadane-style transitions and multiply the current element only in the middle state. Track the maximum value seen across states.
What is the best approach for Maximum Subarray Sum After Multiplier?
The optimal approach uses dynamic programming with Kadane-style state transitions. Track three states while scanning the array: before applying the multiplier, while applying it, and after it has been used. Each state updates using the previous values in O(1), producing a full O(n) time and O(1) space solution.
Is Maximum Subarray Sum After Multiplier asked at Google/Amazon/Meta?
Variants of this problem appear in interviews at large tech companies because it combines Kadane’s algorithm with dynamic programming state transitions. Interviewers use it to test understanding of subarray optimization and handling a single modification to the array.
What data structure is used in Maximum Subarray Sum After Multiplier?
The solution primarily uses arrays and dynamic programming variables rather than complex data structures. The algorithm keeps a few running state values that represent different phases of applying the multiplier while iterating through the array.
What is the time complexity of Maximum Subarray Sum After Multiplier?
The optimal solution runs in O(n) time because the array is scanned once while maintaining constant DP states. Brute force approaches that try all multiplied subarrays take O(n^3), and improved simulations that fix start and end indices require O(n^2).

Ready to solve this problem?

Practice Maximum Subarray Sum After Multiplier with our built-in code editor and test cases.

Practice on FleetCode