EASY C++ SOLUTION 🚀 || CLEAN CODE ✅  || WITHOUT USING HASMAPS || EXPLAINED APPROACH WITH DRY RUN✨
Intuition\n Describe your first thoughts on how to solve this problem. \nTo make nums1 equivalent to nums2, we need to find the minimum possible value of x such
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo make nums1 equivalent to nums2, we need to find the minimum possible value of x such that after removing two elements from nums1 and adding x to all remaining elements, nums1 becomes equal to nums2.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort both arrays:\n- Sort nums1 and nums2 in ascending order.\n2. Iterate through possible starting indices in nums1:\n- Use a loop to iterate through each possible starting index i in nums1, where i ranges from 0 to n1 - n2, where n1 is the length of nums1 and n2 is the length of nums2.\n3. Calculate adjustment needed:\n- For each starting index i, calculate the adjustment needed as the difference between the first element of nums2 and the element at index i in nums1. Let\'s call this adjustment delta.\n4. Check if adjustment is valid by comparing corresponding elements:\n- For each corresponding index j in nums2, check if nums1[i + j] + delta equals nums2[j]. If any of these comparisons fail, mark the adjustment as invalid.\n5. Update minimum adjustment if valid:\n- If the adjustment is valid for all corresponding elements, update the minimum adjustment found so far. This involves comparing the current adjustment with the minimum adjustment found and updating it accordingly.\n6. Return minimum adjustment found, or 0 if no valid adjustment:\n- After iterating through all starting indices and adjustments, return the minimum adjustment found. If no valid adjustment is found, return 0.\n\n# DRYRUN\ninput nums1 = [4,20,16,12,8] and nums2 = [14,18,10].\n\n1. Initialization:\n- nums1 = [4,20,16,12,8], nums2 = [14,18,10]\n- Sorting both arrays: nums1 = [4, 8, 12, 16, 20], nums2 = [10, 14, 18]\n- minAdjustment = INT_MAX\n- n1 = 5, n2 = 3\n2. Loop Iteration 1 (i = 0):\n- Calculate adjustment: adjustment = nums2[0] - nums1[0] = 10 - 4 = 6\n- Initialize isValidAdjustment to true\n- Make a copy of nums1: nums1Adjusted = [4, 8, 12, 16, 20]\n- Check if all corresponding elements can be adjusted:\n    - For j = 0, nums2[j] - adjustment = 10 - 6 = 4, which exists in nums1Adjusted, so continue.\n    - For j = 1, nums2[j] - adjustment = 14 - 6 = 8, which exists in nums1Adjusted, so continue.\n    - For j = 2, nums2[j] - adjustment = 18 - 6 = 12, which exists in nums1Adjusted, so continue.\n- Update minAdjustment to min(6, INT_MAX), so minAdjustment = 6.\n3. Loop Iteration 2 (i = 1):\n- Calculate adjustment: adjustment = nums2[0] - nums1[1] = 10 - 8 = 2\n- Initialize isValidAdjustment to true\nMake a copy of nums1: nums1Adjusted = [4, 8, 12, 16, 20]\n- Check if all corresponding elements can be adjusted:\n    - For j = 0, nums2[j] - adjustment = 10 - 2 = 8, which exists in nums1Adjusted, so continue.\n    - For j = 1, nums2[j] - adjustment = 14 - 2 = 12, which exists in nums1Adjusted, so continue.\n    - For j = 2, nums2[j] - adjustment = 18 - 2 = 16, which exists in nums1Adjusted, so continue.\n- Update minAdjustment to min(2, 6), so minAdjustment = 2.\n4. Loop Iteration 3 (i = 2):\n- Calculate adjustment: adjustment = nums2[0] - nums1[2] = 10 - 12 = -2\n- Initialize isValidAdjustment to true\n- Make a copy of nums1: nums1Adjusted = [4, 8, 12, 16, 20]\n- Check if all corresponding elements can be adjusted:\n    - For j = 0, nums2[j] - adjustment = 10 - (-2) = 12, which exists in nums1Adjusted, so continue.\n    - For j = 1, nums2[j] - adjustment = 14 - (-2) = 16, which exists in nums1Adjusted, so continue.\n    For j = 2, nums2[j] - adjustment = 18 - (-2) = 20, which exists in nums1Adjusted, so continue.\n- Update minAdjustment to min(-2, 2), so minAdjustment = -2.\n5. Result:\n- Return minAdjustment = -2.\n\nSo, the output for the input nums1 = [4,20,16,12,8] and nums2 = [14,18,10] is -2.\n# Complexity\n- Time complexity: **O(N * M)**\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    int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {\n        // Sorting nums1 and nums2\n        sort(nums1.begin(), nums1.end()); \n        sort(nums2.begin(), nums2.end()); \n        \n        int minAdjustment = INT_MAX;\n        int n1 = nums1.size(); \n        int n2 = nums2.size(); \n        \n        // Iterate through all possible combinations of removing elements from nums1\n        for (int i = 0; i <= n1 - n2; ++i) {\n            // Calculate the adjustment needed for this combination\n            int adjustment = nums2[0] - nums1[i]; \n            \n            // Flag to check if adjustment is valid\n            bool isValidAdjustment = true; \n            \n            // Make a copy of nums1 to preserve the original\n            vector<int> nums1Adjusted(nums1); \n            \n            // Check if all corresponding elements can be adjusted\n            for (int j = 0; j < n2; ++j) {\n                auto it = find(nums1Adjusted.begin(), nums1Adjusted.end(), nums2[j] - adjustment);\n                \n                // If adjustment not possible, mark as invalid\n                if (it == nums1Adjusted.end()) {\n                    isValidAdjustment = false; \n                    break;\n                }\n                \n                // Remove used element\n                nums1Adjusted.erase(it); \n            }\n            \n            // Update minimum adjustment\n            if (isValidAdjustment) {\n                minAdjustment = min(adjustment, minAdjustment); \n            }\n        }\n        \n        // Return minimum adjustment found or 0 if no valid adjustment found\n        return minAdjustment == INT_MAX ? 0 : minAdjustment;\n    }\n};\n```
['Sorting', 'C++']