Maximize Active Section with Trade II - Problem

You are given a binary string s of length n, where:

  • '1' represents an active section
  • '0' represents an inactive section

You can perform at most one trade to maximize the number of active sections in s. In a trade, you:

  1. Convert a contiguous block of '1's that is surrounded by '0's to all '0's
  2. Afterward, convert a contiguous block of '0's that is surrounded by '1's to all '1's

Additionally, you are given a 2D array queries, where queries[i] = [li, ri] represents a substring s[li...ri].

For each query, determine the maximum possible number of active sections in s after making the optimal trade on the substring s[li...ri].

Return an array answer, where answer[i] is the result for queries[i].

Note: For each query, treat s[li...ri] as if it is augmented with a '1' at both ends, forming t = '1' + s[li...ri] + '1'. The augmented '1's do not contribute to the final count. The queries are independent of each other.

Input & Output

Example 1 — Basic Trade
$ Input: s = "10110", queries = [[1,3]]
Output: [3]
💡 Note: Query [1,3] gives substring "011". Augmented: "1011". We can trade the '1' block (position 0) with '00' block (positions 1-2) to get "100" → 3 ones total in original string.
Example 2 — Multiple Queries
$ Input: s = "1100", queries = [[0,1],[2,3]]
Output: [2,2]
💡 Note: Query [0,1]: "11" → augmented "111", no beneficial trade. Query [2,3]: "00" → augmented "100", can convert to "111" for 2 total ones.
Example 3 — No Trade Needed
$ Input: s = "111", queries = [[0,2]]
Output: [3]
💡 Note: All positions are already '1', no trade can improve the count.

Constraints

  • 1 ≤ s.length ≤ 105
  • s[i] is either '0' or '1'
  • 1 ≤ queries.length ≤ 105
  • 0 ≤ li ≤ ri < s.length

Visualization

Tap to expand
Maximize Active Section with Trade II INPUT Binary String s = "10110" 1 0 1 1 0 0 1 2 3 4 Query: [1, 3] Substring: s[1..3] = "011" Augmented: t = "1" + "011" + "1" 1 0 1 1 1 (gray = augmented padding) Active (1) Inactive (0) ALGORITHM STEPS 1 Find Blocks Identify contiguous 0s and 1s 0 11 2 Trade Operation Convert 1s-->0s, then 0s-->1s 1-->0: Remove 1-block 0-->1: Fill 0-block 3 Preprocess Store block sizes for queries Block Map: {pos: size} 0-block at idx 1, size=1 4 Calculate Max Original 1s + gained from trade Base: 3 ones in "10110" Trade: fill 0 at idx 1 Result: 3 active sections FINAL RESULT Optimal Configuration Before: "10110" (3 active) 1 0 1 1 0 Trade in query range [1,3] After: "11110" (Max possible: 3) 1 1 1 1 0 (idx 1 converted 0-->1) Output [3] OK Key Insight: The trade operation is optimal when we convert a 0-block surrounded by 1s, maximizing the gain. Preprocessing block positions and sizes allows O(1) query responses. The augmented '1' padding ensures boundary 0-blocks can be filled, simulating surrounded condition. TutorialsPoint - Maximize Active Section with Trade II | Optimized with Preprocessing
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
23.2K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen