Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
Return the maximum possible size of the set s.
Example 1:
Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
Output: 2
Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.
Example 2:
Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
Output: 5
Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.
Example 3:
Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
Output: 6
Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.
Constraints:
n == nums1.length == nums2.length1 <= n <= 2 * 104n is even.1 <= nums1[i], nums2[i] <= 109Problem summary: You are given two 0-indexed integer arrays nums1 and nums2 of even length n. You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s. Return the maximum possible size of the set s.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,2,1,2] [1,1,1,1]
[1,2,3,4,5,6] [2,3,2,3,2,3]
[1,1,2,2,3,3] [4,4,5,5,6,6]
intersection-of-two-arrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
class Solution {
public int maximumSetSize(int[] nums1, int[] nums2) {
Set<Integer> s1 = new HashSet<>();
Set<Integer> s2 = new HashSet<>();
for (int x : nums1) {
s1.add(x);
}
for (int x : nums2) {
s2.add(x);
}
int n = nums1.length;
int a = 0, b = 0, c = 0;
for (int x : s1) {
if (!s2.contains(x)) {
++a;
}
}
for (int x : s2) {
if (!s1.contains(x)) {
++b;
} else {
++c;
}
}
a = Math.min(a, n / 2);
b = Math.min(b, n / 2);
return Math.min(a + b + c, n);
}
}
// Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
func maximumSetSize(nums1 []int, nums2 []int) int {
s1 := map[int]bool{}
s2 := map[int]bool{}
for _, x := range nums1 {
s1[x] = true
}
for _, x := range nums2 {
s2[x] = true
}
a, b, c := 0, 0, 0
for x := range s1 {
if !s2[x] {
a++
}
}
for x := range s2 {
if !s1[x] {
b++
} else {
c++
}
}
n := len(nums1)
a = min(a, n/2)
b = min(b, n/2)
return min(a+b+c, n)
}
# Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
class Solution:
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
s1 = set(nums1)
s2 = set(nums2)
n = len(nums1)
a = min(len(s1 - s2), n // 2)
b = min(len(s2 - s1), n // 2)
return min(a + b + len(s1 & s2), n)
// Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
// class Solution {
// public int maximumSetSize(int[] nums1, int[] nums2) {
// Set<Integer> s1 = new HashSet<>();
// Set<Integer> s2 = new HashSet<>();
// for (int x : nums1) {
// s1.add(x);
// }
// for (int x : nums2) {
// s2.add(x);
// }
// int n = nums1.length;
// int a = 0, b = 0, c = 0;
// for (int x : s1) {
// if (!s2.contains(x)) {
// ++a;
// }
// }
// for (int x : s2) {
// if (!s1.contains(x)) {
// ++b;
// } else {
// ++c;
// }
// }
// a = Math.min(a, n / 2);
// b = Math.min(b, n / 2);
// return Math.min(a + b + c, n);
// }
// }
// Accepted solution for LeetCode #3002: Maximum Size of a Set After Removals
function maximumSetSize(nums1: number[], nums2: number[]): number {
const s1: Set<number> = new Set(nums1);
const s2: Set<number> = new Set(nums2);
const n = nums1.length;
let [a, b, c] = [0, 0, 0];
for (const x of s1) {
if (!s2.has(x)) {
++a;
}
}
for (const x of s2) {
if (!s1.has(x)) {
++b;
} else {
++c;
}
}
a = Math.min(a, n >> 1);
b = Math.min(b, n >> 1);
return Math.min(a + b + c, n);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.