Talentd
FleetCode
Make and follow sheets.Sign in

Maximum Pulse Value After One Subarray Rotation solution and explanation

MediumArrayDynamic ProgrammingPrefix Sum3 min read38.9% acceptanceUpdated 9 Oct 2026
Leetcode 4058 | Maximum Pulse Value After One Subarray Rotation | Leetcode weekly contest 520CodeWithMeGuys · 31:03 · 992 viewsWatch on YouTube
5 more videos on this problem
  1. Leetcode 4058 | Maximum Pulse Value After One Subarray Rotation | Leetcode weekly contest 520CodeWithMeGuys · 31:03 · 992 views
  2. Super Hard💀DSA Question asked by Leetcode in Weekly Contest 520(Q3,4058)Kumar K [Amazon] · 29:00 · 922 views
  3. Maximum Pulse Value After One Subarray Rotation | Leetcode 4058| Leetcode Contest 520Sanyam IIT Guwahati · 21:40 · 627 views
  4. Maximum Pulse Value After One Subarray Rotation | Observation + Kadane'sManjeet Dhayal · 10:10 · 474 views
  5. 4058. Maximum Pulse Value After One Subarray Rotation (Leetcode Medium)Programming Live with Larry · 23:23 · 346 views
  6. Q3. Maximum Pulse Value After One Subarray Rotation | Weekly Contest 520 | LeetCodeaniketdevlp · 17:17 · 233 views

The problem

You are given an integer array nums of length n.

Define the pulse value of an integer array arr as the alternating sum starting at index 0: pulse(arr) = arr[0] - arr[1] + arr[2] - arr[3] + ....

You may perform at most one operation on nums:

  • Choose two indices l and r such that 0 <= l < r < n.
  • Left-rotate the subarray nums[l..r] by exactly one position. For example, [a, b, c, d] becomes [b, c, d, a].

Return the maximum pulse value that can be obtained after performing at most one such operation.

Example 1:

Input: nums = [1,5,2]

Output: 6

Explanation:

  • The original pulse value is 1 - 5 + 2 = -2.
  • Rotate the subarray nums[0..1] from [1, 5] to [5, 1].
  • The resulting array is [5, 1, 2] and its pulse value is 5 - 1 + 2 = 6, which is the maximum possible.

Example 2:

Input: nums = [6,4,3]

Output: 7

Explanation:

  • The original pulse value is 6 - 4 + 3 = 5.
  • Rotate the subarray nums[1..2] from [4, 3] to [3, 4].
  • The resulting array is [6, 3, 4] and its pulse value is 6 - 3 + 4 = 7, which is the maximum possible.

Example 3:

Input: nums = [9,7]

Output: 2

Explanation:

The original pulse value is 9 - 7 = 2, which is already maximum. Thus, no rotation is required.

Constraints:

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

Approach overview

Problem Overview: You get an array and may rotate exactly one contiguous subarray once. Choose the rotation that gives the maximum pulse value, which is computed from the array after the rotation.

Approach 1: Brute Force Over Every Subarray (O(n^3) time, O(1) space)

Enumerate every subarray [l, r] and every valid rotation amount. For each candidate, build the rotated array and compute its pulse value from scratch. This is correct because it checks every possible move. It only works for small inputs, since the rebuild and recomputation repeat work across overlapping subarrays.

Approach 2: Prefix Sums to Evaluate Each Candidate in O(1) (O(n^2) time, O(n) space)

Precompute prefix sums so any segment's contribution is available in constant time. Fix the left boundary and extend the right boundary, updating the candidate value incrementally. The key insight is that a rotation only changes the boundaries of its segment, so the value of everything outside it can come from prefix arrays rather than being recomputed. Prefer this when you need a correct answer quickly and n is in the low thousands.

Approach 3: Single-Pass Dynamic Programming with Prefix Sums (O(n) time, O(n) space)

Scan the array once and track the best value for each state: no rotation started, inside the rotated subarray, and rotation finished. Each state transitions from the previous index using prefix sums to account for the segment's contribution. This removes the nested loop entirely. Use it when n can reach 10^5 or more, which is the normal constraint for this problem.

Recommended for interviews: Start by describing the brute force so the interviewer sees you understand the search space. Then move to prefix sums and state-based DP, which is the optimal solution. The brute force shows understanding, but the single-pass DP shows skill. Explain the state definitions before writing code, because clear states are what interviewers grade most.

Related topics: array, prefix sum, dynamic programming.

The written solution for this problem is still being prepared. Try it yourself in the meantime.

Try solving it yourself

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute ForceO(n^3)O(1)Small inputs, verifying correctness
Prefix Sums with EnumerationO(n^2)O(n)Moderate n, intermediate step
Single-Pass DP with Prefix SumsO(n)O(n)Large n, production and interviews

Frequently asked questions

Is Maximum Pulse Value After One Subarray Rotation easy or hard?

It is a medium-difficulty problem with a 38.9% acceptance rate. The brute force is straightforward, but getting to the O(n) DP requires defining states carefully and handling the rotation boundaries correctly.

How to solve Maximum Pulse Value After One Subarray Rotation in O(n)?

Precompute prefix sums, then iterate once while maintaining DP states for before, during, and after the rotated subarray. At each index, update each state using the previous index's best values and keep the global maximum. Return the maximum value seen across valid final states.

Maximum Pulse Value After One Subarray Rotation Python and Java solution?

Both languages use the same single-pass DP. In Python, build the prefix array with itertools.accumulate and update three state variables in a loop. In Java, use a long prefix array to avoid overflow and update the same three states with a for loop.

What is the best approach for Maximum Pulse Value After One Subarray Rotation?

The best approach is a single-pass dynamic programming solution using prefix sums. Track the best value for each state (no rotation yet, inside the rotated subarray, rotation finished) as you scan once through the array.

What data structure is used in Maximum Pulse Value After One Subarray Rotation?

The solution uses an array for input and prefix sums to get segment contributions in constant time. Dynamic programming stores a few scalar state values per index, so extra space is O(n) for the prefix array or O(1) if you roll the state variables.

What is the time complexity of Maximum Pulse Value After One Subarray Rotation?

The optimal solution runs in O(n) time. The brute force that checks every subarray and rotation runs in O(n^3), and a prefix-sum enumeration runs in O(n^2).

Is Maximum Pulse Value After One Subarray Rotation asked at Google, Amazon, or Meta?

Company tags for LeetCode problems change over time and vary by source, so check the company filter on FleetCode for current data. The problem's DP and prefix sum tags make it a common medium-level interview question at large tech companies.

Comments

Now write it yourselfClose this page and solve Maximum Pulse Value After One Subarray Rotation in the editor, without looking.
Practise Maximum Pulse Value After One Subarray Rotation