Maximize Subarrays After Removing One Conflicting Pair - Problem

You are given an integer n which represents an array nums containing the numbers from 1 to n in order. Additionally, you are given a 2D array conflictingPairs, where conflictingPairs[i] = [a, b] indicates that a and b form a conflicting pair.

Remove exactly one element from conflictingPairs. Afterward, count the number of non-empty subarrays of nums which do not contain both a and b for any remaining conflicting pair [a, b].

Return the maximum number of subarrays possible after removing exactly one conflicting pair.

Input & Output

Example 1 — Basic Case
$ Input: n = 5, conflictingPairs = [[1,5], [2,3]]
Output: 14
💡 Note: Remove [2,3] to get max subarrays. With only [1,5] remaining, we avoid subarrays containing both 1 and 5. Total subarrays = 15. Invalid subarrays containing both 1 and 5: start ≤ 1 and end ≥ 5, giving 1×1 = 1 invalid subarray. Valid count: 15 - 1 = 14.
Example 2 — Single Pair
$ Input: n = 4, conflictingPairs = [[2,4]]
Output: 10
💡 Note: Only one pair to remove [2,4]. Total subarrays = 4×5/2 = 10. After removal, no constraints remain, so all 10 subarrays are valid.
Example 3 — Multiple Pairs
$ Input: n = 3, conflictingPairs = [[1,2], [2,3]]
Output: 4
💡 Note: Remove [1,2] keeps [2,3]. Invalid subarrays contain both 2 and 3: positions 2×2=4, but total is only 6, so 6-2=4 valid. Remove [2,3] keeps [1,2]: 6-2=4 valid. Both give same result.

Constraints

  • 2 ≤ n ≤ 105
  • 1 ≤ conflictingPairs.length ≤ 105
  • conflictingPairs[i].length == 2
  • 1 ≤ conflictingPairs[i][0], conflictingPairs[i][1] ≤ n
  • conflictingPairs[i][0] ≠ conflictingPairs[i][1]

Visualization

Tap to expand
Maximize Subarrays After Removing One Conflicting Pair INPUT Array nums (n=5): 1 2 3 4 5 idx 0 idx 1 idx 2 idx 3 idx 4 Conflicting Pairs: Pair 1: [1, 5] Pair 2: [2, 3] Subarrays cannot contain both elements of a pair n=5, pairs=[[1,5],[2,3]] ALGORITHM STEPS 1 Calculate Base Count Count valid subarrays with all pairs active 2 Track Blocked Subarrays For each pair, find subarrays it blocks 3 Calculate Gain Subarrays gained by removing each pair 4 Find Maximum Remove pair giving max total subarrays Mathematical Formula: Total = n*(n+1)/2 = 15 Max = base + max(gain) gain[i] = blocked by pair i FINAL RESULT Best: Remove [1, 5] Valid Subarrays (9 total): [1] [2] [3] [4] [5] [1,2] [3,4] [4,5] [3,4,5] Pair [2,3] still blocks: [2,3], [1,2,3], [2,3,4], etc. OUTPUT 9 OK - Maximum Found! 9 valid subarrays achieved Key Insight: The mathematical formula approach tracks which subarrays are blocked by each conflicting pair. By removing the pair that blocks the most subarrays (that aren't blocked by other pairs), we maximize the count. Time complexity: O(n + m) where m = number of conflicting pairs. TutorialsPoint - Maximize Subarrays After Removing One Conflicting Pair | Mathematical Formula Approach
Asked in
Google 25 Microsoft 15 Amazon 10
27.7K 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