3002. Maximum Size of a Set After Removals LeetCode Solution

In this guide, you will get 3002. Maximum Size of a Set After Removals LeetCode Solution with the best time and space complexity. The solution to Maximum Size of a Set After Removals problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Size of a Set After Removals solution in C++
  4. Maximum Size of a Set After Removals solution in Java
  5. Maximum Size of a Set After Removals solution in Python
  6. Additional Resources
3002. Maximum Size of a Set After Removals LeetCode Solution image

Problem Statement of Maximum Size of a Set After Removals

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.

See also  2670. Find the Distinct Difference Array LeetCode Solution

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.length
1 <= n <= 2 * 104
n is even.
1 <= nums1[i], nums2[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3002. Maximum Size of a Set After Removals LeetCode Solution in C++

class Solution {
 public:
  int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
    const unordered_set<int> set1{nums1.begin(), nums1.end()};
    const unordered_set<int> set2{nums2.begin(), nums2.end()};
    unordered_set<int> common;

    for (const int num1 : set1)
      if (set2.contains(num1))
        common.insert(num1);

    const int n = nums1.size();
    const int n1 = set1.size();
    const int n2 = set2.size();
    const int nc = common.size();
    const int maxUniqueNums1 = min(n1 - nc, n / 2);
    const int maxUniqueNums2 = min(n2 - nc, n / 2);
    return min(n, maxUniqueNums1 + maxUniqueNums2 + nc);
  }
};
/* code provided by PROGIEZ */

3002. Maximum Size of a Set After Removals LeetCode Solution in Java

class Solution {
  public int maximumSetSize(int[] nums1, int[] nums2) {
    Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
    Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet());
    Set<Integer> common = new HashSet<>();

    for (final int num1 : set1)
      if (set2.contains(num1))
        common.add(num1);

    final int n = nums1.length;
    final int n1 = set1.size();
    final int n2 = set2.size();
    final int nc = common.size();
    final int maxUniqueNums1 = Math.min(n1 - nc, n / 2);
    final int maxUniqueNums2 = Math.min(n2 - nc, n / 2);
    return Math.min(n, maxUniqueNums1 + maxUniqueNums2 + nc);
  }
}
// code provided by PROGIEZ

3002. Maximum Size of a Set After Removals LeetCode Solution in Python

class Solution:
  def maximumSetSize(self, nums1: list[int], nums2: list[int]) -> int:
    set1 = set(nums1)
    set2 = set(nums2)
    common = set1.intersection(set2)

    n = len(nums1)
    n1 = len(set1)
    n2 = len(set2)
    nc = len(common)
    maxUniqueNums1 = min(n1 - nc, n // 2)
    maxUniqueNums2 = min(n2 - nc, n // 2)
    return min(n, maxUniqueNums1 + maxUniqueNums2 + nc)
# code by PROGIEZ

Additional Resources

See also  2470. Number of Subarrays With LCM Equal to K LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.