3132. Find the Integer Added to Array II
\n# Complexity\n- Time complexity:O(nlogn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n) \n
\n# Complexity\n- Time complexity:$$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool possible(vector<int> &nums1, vector<int> &nums2, int k){\n        map<int, int> mp;\n        for(auto x : nums1){\n            mp[(x + k)]++;\n        }\n        int count = 0;\n        for(auto x : nums2){\n            if(mp.find(x) == mp.end()) return false;\n            else{\n                mp[x]--;\n                if(mp[x] == 0) mp.erase(x);\n                count++;\n            }\n        }\n        return count == nums2.size();\n        \n    }\n    int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {\n        int ans = 1000;\n        sort(nums1.begin(), nums1.end());\n        sort(nums2.begin(), nums2.end());\n\n        if(possible(nums1, nums2, nums2[0] - nums1[0])){\n            ans = min(ans,nums2[0] - nums1[0] );\n        }\n        if(possible(nums1, nums2, nums2[0] - nums1[1])){\n            ans = min(ans,nums2[0] - nums1[1] );\n        }\n        if(possible(nums1, nums2, nums2[0] - nums1[2])){\n            ans = min(ans,nums2[0] - nums1[2] );\n        }\n        return ans;\n    }\n};\n```
['C++']