Java Sorting with two pointer O(nlogn)
Intuition\nFirst, we sort both of the arrays. Then the question becomes, would we use the smallest element\'s differences or the second smallest or use them bot
# Intuition\nFirst, we sort both of the arrays. Then the question becomes, would we use the smallest element\'s differences or the second smallest or use them both as the two removed elements?\n\nIf we choose the smallest element\'s difference, we would have to remove two elements later on, whereas in the other case, we would have fewer removed elements.\n\n- i = 0: we will need to remove two elements\n- i = 1: we will need to remove one element\n- i = 2: we have skipped over 0 and 1 there for 0 element needs to be removed\n\nAnytime we need to skip over more elements, then the current condition is not valid solution, as out of the three, there would be at least one valid condition based on the problem constraints.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumAddedInteger(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        \n        int min = 2000;\n        for(int i=0;i<=2;i++){\n            int skip = i;\n            int diff = nums2[0] - nums1[i];\n            int k = 0;\n            for(int j=i;j<nums1.length;j++){\n                if((nums2[k] - nums1[j]) != diff && skip == 2)break;\n                if((nums2[k] - nums1[j]) != diff){\n                    skip++;\n                }else{\n                    k++;\n                }\n                if(k == nums2.length)break;\n            }\n            if(k == nums2.length){\n                min = Math.min(min,diff);      \n            }\n        }\n        return min == 2000?0:min;\n    }\n}\n```
['Two Pointers', 'Sorting', 'Java']