✅Find the Integer Added to Array II💯💫🚀
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the minimum possible integer x such that after remov
![Screenshot 2024-04-28 at 9.58.49\u202FAM.png](https://assets.leetcode.com/users/images/514d3797-258a-4cd5-a171-9801bdff9727_1714279067.6967068.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the minimum possible integer `x` such that after removing two elements from `nums1` and adding `x` to the remaining elements, `nums1` becomes equal to `nums2`. One way to approach this is to iterate through the elements of both arrays and find the common difference between corresponding elements. We can handle different cases based on the position of the elements.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort both `nums1` and `nums2`.\n2. Calculate the difference `d` between the first elements of the arrays after sorting.\n3. Check if all the corresponding differences are equal to `d`. If so, return `d`.\n4. If not, check if all the corresponding differences except for the first element are equal to `d`. If so, return `d`.\n5. If none of the above conditions are met, return the difference between the first elements of the arrays.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n \\log n)$$, where `n` is the size of the arrays. Sorting takes $$O(n \\log n)$$ time.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {\n        sort(nums1.begin(), nums1.end());\n        sort(nums2.begin(), nums2.end());\n        int d = nums2[0] - nums1[2];\n        const int n = nums2.size();\n        bool ok = true;\n        for (int i = 1; ok && i < n; ++i) {\n            ok = nums2[i] - nums1[i + 2] == d;\n        }\n        if (ok) return d;\n        d = nums2[0] - nums1[1];\n        ok = true;\n        for (int i = 1, j = 2, c = 0; ok && i < n; ++i, ++j) {\n            if (nums2[i] - nums1[j] != d) {\n                if  (++c > 1) {\n                    ok = false;\n                } else {\n                    --i;\n                }\n            }\n            \n        }\n        if (ok) return d;\n        return nums2[0] - nums1[0];\n    }\n};\n```\n```Python []\nclass Solution:\n    def minimumAddedInteger(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        \n        d = nums2[0] - nums1[2]\n        n = len(nums2)\n        ok = True\n        \n        for i in range(1, n):\n            if nums2[i] - nums1[i + 2] != d:\n                ok = False\n                break\n        \n        if ok:\n            return d\n        \n        d = nums2[0] - nums1[1]\n        ok = True\n        \n        for i in range(1, n):\n            if nums2[i] - nums1[i + 1] != d:\n                if c + 1 > 1:\n                    ok = False\n                else:\n                    c += 1\n                    i -= 1\n        \n        if ok:\n            return d\n        \n        return nums2[0] - nums1[0]\n```\n```Java []\nimport java.util.Arrays;\n\nclass Solution {\n    public int minimumAddedInteger(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        \n        int d = nums2[0] - nums1[2];\n        int n = nums2.length;\n        boolean ok = true;\n        \n        for (int i = 1; i < n; i++) {\n            if (nums2[i] - nums1[i + 2] != d) {\n                ok = false;\n                break;\n            }\n        }\n        \n        if (ok) {\n            return d;\n        }\n        \n        d = nums2[0] - nums1[1];\n        ok = true;\n        \n        for (int i = 1, c = 0; i < n; i++) {\n            if (nums2[i] - nums1[i + 1] != d) {\n                if (++c > 1) {\n                    ok = false;\n                } else {\n                    c++;\n                    i--;\n                }\n            }\n        }\n        \n        if (ok) {\n            return d;\n        }\n        \n        return nums2[0] - nums1[0];\n    }\n}\n
['C++']