Beats 100% of users || Step by Step Explain || Brute Force Approach || Easy to Understand ||
Abhiraj Pratap Singh\n\n---\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The goal appears to be finding the difference betw
# Abhiraj Pratap Singh\n\n---\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The goal appears to be finding the difference between two sets of integers represented by vectors nums1 and nums2. The difference is computed as the elements present in nums1 but not in nums2 (ans1) and the elements present in nums2 but not in nums1 (ans2).\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The code uses sets (s1 and s2) to convert vectors nums1 and nums2 into sets to efficiently perform set operations.\n2. It then calculates the set differences using set_difference between s1 and s2 (ans1) and between s2 and s1 (ans2).\n3. The result is stored in vectors v1 and v2.\n4. Finally, the function returns a vector of vectors containing the results v1 and v2.\n\n---\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**Time complexity: O(m+n),** where m and n are the sizes of nums1 and nums2, respectively. The set differences take linear time.\n\n---\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**Space complexity: O(m+n),** where m and n are the sizes of nums1 and nums2, respectively. The space is used to store sets and vectors.\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) \n    {\n        set<int>s1(nums1.begin(),nums1.end());\n        set<int>s2(nums2.begin(),nums2.end());\n        set<int>ans1,ans2;\n        vector<int>v1;\n        vector<int>v2;\n\n        set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(ans1,ans1.end()));\n        set_difference(s2.begin(),s2.end(),s1.begin(),s1.end(),inserter(ans2,ans2.end()));\n\n        for(auto it :ans1)\n            v1.push_back(it);\n        for(auto it :ans2)\n            v2.push_back(it);\n        return {v1,v2};\n    }\n};\n```\n\n---\n\n# IF YOU LIKE THE SOLUTION PLEASE UPVOTE IT....\n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/ffbca146-9aea-40ba-8ebc-8b8dfc61b5f2_1704482170.9687743.gif)\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/32a64a50-aa40-4f39-a01f-290e651bb458_1704482174.0209467.gif)\n
['Array', 'Hash Table', 'Ordered Set', 'C++']