Greedy Picker | C++ | Java
Intuition\nWe have to somehow keep first pair with two maximum values in the given array and second pair with two minimum values in the given array in ordered t
# Intuition\nWe have to somehow keep first pair with two maximum values in the given array and second pair with two minimum values in the given array in ordered to maximize the product difference between two pairs.\n\n# Approach\nSort the given array in asceding or descending order whichever you may prefer and pick the two pairs greedily in the way discussed above in the intuition section.\n\n# Complexity\n- Time complexity:\nO(n * log n), n is the size of given array, this complexity is because of sorting we have used.\n\n- Space complexity:\nO(1)\n\n# Code\nC++\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int sz = nums.size();\n        //picking pairs greedily\n        return (nums[sz-1] * nums[sz - 2]) - (nums[0] * nums[1]);\n    }\n};\n```\nJava\n```\nclass Solution {\n    public int maxProductDifference(int[] nums) {\n        Arrays.sort(nums);\n        int sz = nums.length;\n        //picking pairs greedily\n        return (nums[sz-1] * nums[sz - 2]) - (nums[0] * nums[1]);\n    }\n}\n```
['C++', 'Java']