Maximize Win From Two Segments - Problem

There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line.

You are also given an integer k.

You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments).

The two selected segments may intersect.

For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.

Return the maximum number of prizes you can win if you choose the two segments optimally.

Input & Output

Example 1 — Basic Case
$ Input: prizePositions = [1,1,2,2,3,3,5], k = 2
Output: 7
💡 Note: Choose segments [1,3] and [3,5]. Segment [1,3] covers prizes at positions 1,1,2,2,3,3 (6 prizes). Segment [3,5] covers prizes at positions 3,3,5 (3 prizes). Union covers all 7 prizes at positions 1,1,2,2,3,3,5.
Example 2 — Non-overlapping Optimal
$ Input: prizePositions = [1,2,3,4], k = 1
Output: 4
💡 Note: Choose segments [1,2] and [3,4]. First segment covers positions 1,2 (2 prizes), second segment covers positions 3,4 (2 prizes). Total: 4 prizes.
Example 3 — Single Position Multiple Prizes
$ Input: prizePositions = [1,1,1,1], k = 0
Output: 4
💡 Note: Both segments can cover position [1,1], capturing all 4 prizes at position 1.

Constraints

  • 1 ≤ prizePositions.length ≤ 105
  • 1 ≤ prizePositions[i] ≤ 109
  • 0 ≤ k ≤ 109
  • prizePositions is sorted in non-decreasing order

Visualization

Tap to expand
Maximize Win From Two Segments INPUT 1 2 3 5 prizePositions: 1 1 2 2 3 3 5 k = 2 Segment length = k Select 2 segments to maximize prizes ALGORITHM STEPS 1 Binary Search Setup For each pos, find rightmost prize within [pos, pos+k] 2 Compute dp[] array dp[i] = max prizes from single segment ending at i 3 Track max prefix maxBefore[i] = best single segment up to position i 4 Combine two segments ans = max(current_segment + maxBefore[start-1]) Two-Pass Approach: Pass 1: Build dp[] for single segment max Pass 2: Combine with prefix max for 2 segments FINAL RESULT Segment 1: [1,3] Segment 2: [3,5] 1 2 3 5 Prizes Collected: Segment [1,3]: 6 prizes - Pos 1: 2 prizes - Pos 2: 2 prizes - Pos 3: 2 prizes (Pos 5 not needed for max) Output: 6 Key Insight: Use binary search to find segment boundaries efficiently. Track the maximum prizes achievable with one segment ending at or before each position (prefix max). For each position as segment end, combine current segment prizes with best previous non-overlapping segment. Time: O(n log n) TutorialsPoint - Maximize Win From Two Segments | Two-Pass with Binary Search Approach
Asked in
Google 25 Amazon 18 Microsoft 15
23.0K Views
Medium Frequency
~25 min Avg. Time
856 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