Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Dynamic Programming
3499. Maximize Active Section with Trade I

Choose a trade operation to maximize the size or score of an active section in an array.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

LeetCode 3499: Maximize Active Section with Trade I

gfg
Problem Statement

Problem

You are given an array that represents sections in an active/inactive state. You may perform at most one trade operation that changes the state of a contiguous section in a way that can improve the total active result.

Return the maximum active value you can obtain after applying the best possible trade, or choosing to do nothing if that is better.

The exact trade mechanics are problem-specific, but the core task is to evaluate every possible place to apply the operation and maximize the final active section value.

Notes

  • The input is an array-based optimization problem.
  • The intended solution should consider the net gain of applying the trade rather than simulating every possibility naively.
  • A linear or near-linear scan is typically expected for this type of problem.

Input Format

  • An integer array nums describing the initial state or score of each section.
  • One trade operation may be applied according to the problem rules.
  • The operation is optional.

Output Format

  • Return a single integer: the maximum achievable active-section value after at most one trade.

Constraints

  • 1 <= nums.length (exact limits unavailable from the provided metadata)
  • Values are integers
  • A valid answer must consider the option of not applying the trade
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

5

Explanation

One illustrative interpretation is to trade the segment that improves the total active score the most. The best gain comes from a contiguous region, leading to a maximum final value of 5.

Example 2

Input

nums = [4, 1, 2]

Output

7

Explanation

If the trade is not beneficial, keeping the array as-is can be optimal. This example illustrates that the answer is the best achievable result, not necessarily the result of performing the trade.

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.