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.
Example 1:
Input: n = 4, conflictingPairs = [[2,3],[1,4]]
Output: 9
Explanation:
[2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].nums where [1, 4] do not appear together. They are [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3] and [2, 3, 4].conflictingPairs is 9.Example 2:
Input: n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]
Output: 12
Explanation:
[1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].nums where [2, 5] and [3, 5] do not appear together.conflictingPairs is 12.
Constraints:
2 <= n <= 1051 <= conflictingPairs.length <= 2 * nconflictingPairs[i].length == 21 <= conflictingPairs[i][j] <= nconflictingPairs[i][0] != conflictingPairs[i][1]Problem Overview: You are given n elements and a list of conflicting pairs (a, b). Any subarray containing both values of a conflicting pair is invalid. You may remove exactly one conflicting pair from the list. The task is to maximize the number of valid subarrays that remain.
Approach 1: Brute Force Pair Removal + Subarray Validation (O(n2 * m) time, O(1) space)
Try removing each conflicting pair one at a time. For the remaining pairs, enumerate every possible subarray and check whether it contains both elements of any conflict. This requires scanning the subarray or tracking membership, making each validation expensive. The approach demonstrates the problem structure but becomes infeasible when n and the number of pairs grow. It mainly helps you reason about how conflicting pairs limit valid subarray boundaries.
Approach 2: Enumeration + Maintaining Minimum and Second Minimum Values (O(n + m) time, O(n) space)
The key observation: for a subarray ending at position r, the earliest valid start depends on the largest conflicting left endpoint among all pairs whose right endpoint is ≤ r. If the maximum left value is max1, any subarray must start after max1. That gives r - max1 valid subarrays ending at r.
Group conflicts by their right endpoint and iterate r from 1..n. While processing, maintain the largest and second-largest left endpoints seen so far. The largest value defines the current restriction for valid subarrays. The second-largest becomes relevant if the pair causing the largest restriction is removed.
For every index r, add r - max1 to the base answer. The improvement from removing the dominant pair equals max1 - max2, because the boundary shifts from max1 to max2. Accumulate this potential gain for the pair responsible for max1. After scanning the array, add the maximum gain to the base count.
This technique combines ideas from Array enumeration and prefix tracking similar to Prefix Sum style accumulation. The ordering of constraints acts like a simplified structure compared to a full Segment Tree, which would otherwise maintain range restrictions dynamically.
Recommended for interviews: The enumeration with maintained maximum and second maximum constraints is the expected solution. It reduces the problem to a linear scan and demonstrates strong reasoning about how conflicts restrict subarray boundaries. Mentioning the brute force idea first shows you understand the constraint interactions before optimizing.
We store all conflicting pairs (a, b) (assuming a < b) in a list g, where g[a] represents the set of all numbers b that conflict with a.
If no deletion occurs, we can enumerate each subarray's left endpoint a in reverse order. The upper bound of its right endpoint is the minimum value b_1 among all g[x geq a] (excluding b_1), and the contribution to the answer is b_1 - a.
If we delete a conflicting pair containing b_1, then the new b_1 becomes the second minimum value b_2 among all g[x geq a], and its additional contribution to the answer is b_2 - b_1. We use an array cnt to record the additional contribution for each b_1.
The final answer is the sum of all b_1 - a contributions plus the maximum value of cnt[b_1].
The time complexity is O(n), and the space complexity is O(n), where n is the number of conflicting pairs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Removal + Subarray Check | O(n² * m) | O(1) | Useful for understanding the constraint behavior in small inputs |
| Enumeration with Max and Second-Max Conflict Tracking | O(n + m) | O(n) | Optimal solution for large inputs where subarray boundaries depend on prefix conflict limits |
Maximize Subarrays After Removing One Conflicting Pair | Detailed Explanation | Leetcode 3480 | MIK • codestorywithMIK • 10,249 views views
Watch 9 more video solutions →Practice Maximize Subarrays After Removing One Conflicting Pair with our built-in code editor and test cases.
Practice on FleetCode