Simple and Easy Casework
Intuition\nThere can be 2 types of elements in each set,\n\n1. Which are common in both the arrays\n2. Which are distinct in both the arrays\n\n\nNow, suppose t
# Intuition\nThere can be 2 types of elements in each set,\n```\n1. Which are common in both the arrays\n2. Which are distinct in both the arrays\n```\n\nNow, suppose there are **x** elements in set A which are distinct and **y** elements in set B which are distinct and **m** elements arre common in both of them.\nThere can be atmax **4** possible cases:\n\n1. `x>=n/2 and y>=n/2`\n   We can pick only distinct elements from the both the set and **answer=n**.\n2. `x>=n/2 and y<n/2 `\n  We can keep distinct elements in set 1 and common elements + y in set 2. In this way, the **answer= n/2+min(n/2,y+m);**\n3. `x<n/2 and y>=n/2`\n We can keep distinct elements in set 2 and common elements + x in set 1.Then, **answer=n/2+min(n/2,x+m)**;\n4. `x<n/2 and y<n/2`\nWe keep distinct elements of both the elements in combination with the common elements. Then **answer=min(n,x+y+m)**;\n# Approach\nWe will copy all the elements of nums1 and nums2 in 2 sets, in order to have only distinct elements.Then we we will iterate through set1 and check if this particular element is present in set2, simply push it in common and erase from second. Also, then we will iterate through common elements and remove this element from set 1 as well and then we will do the casework which i discussed above.\n\n# Complexity\n- Time complexity:\nSince, we are using only set to erase and insert elements.\n**TC- O(nlogn)**\n\n- Space complexity:\nWe are using 3 distinct sets which will store atmost 2*n elements when all of them are distinct. Hence, **SC=O(2n)**\n\n# Code\n```\n#define all(v) v.begin(),v.end()\n\nclass Solution {\npublic:\n    int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        set<int> st1(all(nums1));\n        set<int> st2(all(nums2));\n        set<int> common;\n        for(auto &it:st1)\n        {\n            if(st2.count(it))\n            {\n                common.insert(it);\n                st2.erase(it);\n            }\n        }\n        for(auto &it:common) st1.erase(it);\n        int siz1=st1.size(),siz2=st2.size(),m=common.size();\n        if(siz1>=n/2 and siz2>=n/2) return n;\n        else if(siz1>=n/2)\n        {\n            int sec=min(siz2+m,n/2);\n            return n/2+sec;\n        }\n        else if(siz2>=n/2)\n        {\n            int sec=min(siz1+m,n/2);\n            return n/2+sec;\n        }\n        else\n        {\n            int ans=min(siz1+siz2+m,n);\n            return ans;\n        }\n    }\n};\n```
['Math', 'C++']