Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Greedy
Google
3501. Maximize Active Section with Trade II

Find the best contiguous section after optionally performing a limited trade operation to maximize the total active value.

Acceptance 0%
Problem Statement

You are given an array of integers representing the value of each section in a line. A contiguous section is considered active if its total contribution is maximized under the allowed trade rule.

At most once, you may perform a trade operation that changes how a chosen segment contributes to the final score. Your goal is to choose the best active section and, if beneficial, apply the trade so that the resulting score is as large as possible.

Return the maximum achievable score.

This is a practice-style rephrasing of the problem: the key task is to optimize over contiguous subarrays while accounting for one optional trade-like transformation.

Input Format

Input

  • An integer array nums.
  • A trade operation is allowed at most once and affects one contiguous segment according to the problem’s rule.

Output

  • Return the maximum achievable score for an active contiguous section.

Output Format

Return a single integer: the maximum score obtainable.

Constraints

  • 1 <= nums.length
  • Values may be positive, negative, or zero.
  • The solution should run in linear or near-linear time for typical interview constraints.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, -2, 3, 4, -1]

Output

7

Explanation

A best active section is [3, 4] with score 7. If the trade rule helps, it should be incorporated into the same contiguous-optimization framework.

Example 2

Input

nums = [-5, -1, -3]

Output

-1

Explanation

When all values are negative, the best choice is the least negative single section.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.