2-liners
We can do it in O(n), but that just longer.\n\n> Update: see O(n) solution below.\n\n#### Approach 1: Sort\n\nC++\ncpp\nint maxProductDifference(vector<int>& nu
We can do it in O(n), but that just longer.\n\n> Update: see O(n) solution below.\n\n#### Approach 1: Sort\n\n**C++**\n```cpp\nint maxProductDifference(vector<int>& nums) {\n    sort(begin(nums), end(nums));\n    return nums.back() * nums[nums.size() - 2] - nums[0] * nums[1];\n}\n```\n**Python 3**\n```python\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        nums.sort()\n        return nums[-1] * nums[-2] - nums[0] * nums[1]\n```\n**Java**\n```java\npublic int maxProductDifference(int[] nums) {\n    Arrays.sort(nums);\n    return nums[nums.length - 1] * nums[nums.length - 2] - nums[0] * nums[1];\n}\n```\n\n#### Approach 2: Nth Element\nThis allows us to move two smallest (order does not matter) and two largest elements to the corresponding ends of our array in O(n).\n```cpp\nint maxProductDifference(vector<int>& nums) {\n    nth_element(begin(nums), begin(nums) + 2, end(nums));\n    nth_element(begin(nums) + 2, begin(nums) + nums.size() - 2, end(nums));\n    return nums.back() * nums[nums.size() - 2] - nums[0] * nums[1];\n}\n```
['C', 'Python', 'Java']