Problem Description
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. The goal is to maximize the size (i.e. the number of distinct elements) of s. Return the maximum possible size of the set s.
Key Insights
- Each array has exactly n/2 elements remaining after removals.
- From each array, you can keep at most n/2 distinct numbers (even if the original array has more unique numbers).
- Many numbers may appear exclusively in one array (exclusive) or in both arrays (common). If a number appears in both arrays, you have flexibility where to “place” it in the kept subsets.
- The high-level idea is to choose, from each array, as many unique (distinct) elements as possible. However, if one array contains too many distinct numbers, you are limited by the n/2 size. Then, you can “fill” the other array’s kept subset with some common numbers from the other array if space allows.
- A greedy / assignment view is useful: assign each number that appears only in one array (exclusive) to that array’s kept subset – subject to a capacity of n/2 per array. Then, assign as many common numbers (appearing in both arrays) as possible into the “remaining capacities” of the two kept subsets. Finally, the answer is the total count of unique numbers you managed to “assign” (with the overall cap coming from the total unique numbers available and the overall 2*(n/2)=n kept elements).
Space and Time Complexity
Time Complexity: O(n) – we traverse the arrays a few times and work with sets/dictionaries. Space Complexity: O(n) – to store the unique elements from each array.
Solution
We first determine the capacity per array which is X = n/2. Then, we compute:
• set1 = unique elements from nums1
• set2 = unique elements from nums2
Let Aonly be the count of numbers in set1 but not in set2, Bonly be the count in set2 but not in set1, and common be the count of numbers present in both sets.
Since each kept array can only hold X elements, we can use:
• effective_A = min(Aonly, X) (the exclusive numbers kept from nums1)
• effective_B = min(Bonly, X) (the exclusive numbers kept from nums2)
The remaining capacity in nums1 is capacity1 = X - effective_A and in nums2 is capacity2 = X - effective_B. We can “fill” these remaining slots (total capacity = capacity1 + capacity2) with common numbers – up to however many are available. Thus, the potential maximum distinct numbers in the union is: union_candidate = effective_A + effective_B + min(common, capacity1 + capacity2).
Since we cannot exceed the total number of unique elements overall, and also the total number of kept elements (which is n), the answer is: answer = min(union_candidate, (Aonly + Bonly + common), n).
This assignment–interpretation ensuring that each exclusive number is “forced” to one array and common numbers are used optimally is the key insight for this problem.