Maximum Size of a Set After Removals - Problem
Maximum Size of a Set After Removals

You're given two integer arrays nums1 and nums2, each of even length n. Your goal is to strategically remove exactly n/2 elements from each array, then combine all remaining elements into a single set.

The challenge: What's the maximum possible size of the final set?

Since we're dealing with a set, duplicate elements are automatically removed. Your task is to decide which elements to keep from each array to maximize the number of unique elements in the final result.

Example: If nums1 = [1,2,3,4] and nums2 = [3,4,5,6], you need to keep 2 elements from each array. The optimal strategy might be to keep [1,2] from nums1 and [5,6] from nums2, resulting in a set of size 4.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums1 = [1,2,3,4], nums2 = [3,4,5,6]
โ€บ Output: 4
๐Ÿ’ก Note: Remove elements {3,4} from nums1 and {3,4} from nums2. The remaining elements are {1,2} from nums1 and {5,6} from nums2. The final set is {1,2,5,6} with size 4.
example_2.py โ€” All Common Elements
$ Input: nums1 = [1,2,1,2], nums2 = [1,1,2,2]
โ€บ Output: 2
๐Ÿ’ก Note: Both arrays contain the same unique elements {1,2}. After removing n/2=2 elements from each array, we can keep at most 2 unique elements total, giving us the set {1,2}.
example_3.py โ€” No Common Elements
$ Input: nums1 = [1,1,3,3], nums2 = [2,2,4,4]
โ€บ Output: 4
๐Ÿ’ก Note: Arrays have no common elements. We can keep all unique elements from both arrays: {1,3} from nums1 and {2,4} from nums2, resulting in set {1,2,3,4} with size 4.

Constraints

  • n == nums1.length == nums2.length
  • 1 โ‰ค n โ‰ค 105
  • n is even
  • 1 โ‰ค nums1[i], nums2[i] โ‰ค 109

Visualization

Tap to expand
๐Ÿ“š Library Book Collection StrategyCollection A๐Ÿ“– Mystery Novel๐Ÿ“– Poetry Book๐Ÿ“– History Book*๐Ÿ“– Science Book**also in Collection BCollection B๐Ÿ“– History Book*๐Ÿ“– Science Book*๐Ÿ“– Art Book๐Ÿ“– Travel Guide*also in Collection AStep 1: Categorize BooksUnique to A๐Ÿ“– Mystery Novel๐Ÿ“– Poetry BookCount: 2Unique to B๐Ÿ“– Art Book๐Ÿ“– Travel GuideCount: 2In Both๐Ÿ“– History Book๐Ÿ“– Science BookCount: 2Step 2: Apply Greedy Strategy (Keep 2 from each collection)From Collection A: Keep 2 booksโœ… Priority: All unique books (Mystery, Poetry)Remaining capacity: 0From Collection B: Keep 2 booksโœ… Priority: All unique books (Art, Travel)Remaining capacity: 0๐Ÿ“š Final Shelf: 4 Unique BooksMystery Novel | Poetry Book | Art Book | Travel Guide
Understanding the Visualization
1
Catalog the Collections
Identify which books are unique to each collection and which books appear in both collections
2
Apply Selection Strategy
Keep all unique books first (they don't create duplicates), then use remaining space for books that appear in both collections
3
Optimize Final Shelf
The greedy approach ensures maximum variety: prioritize uniqueness, avoid unnecessary duplicates
Key Takeaway
๐ŸŽฏ Key Insight: Just like a librarian prioritizes unique books to maximize variety, our algorithm greedily selects unique elements first, then optimally uses remaining capacity for common elements. This simple strategy guarantees the maximum possible set size!
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25
42.8K Views
Medium-High Frequency
~18 min Avg. Time
1.3K 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