Easy to understand O(N) solution | 0 ms, Beats 100% in runtime & 99.72% in memory.
\r\n\r\nTime Complexity: O(N)\r\nSpace Complexity: O(1)\r\n\r\n# Approach 1:\r\njava []\r\nclass Solution {\r\n    public static int max_numbers(int[] arr, int 
![image.png](https://assets.leetcode.com/users/images/05faf66a-a7ef-4d39-b58b-7ecda25c7888_1676948589.2442589.png)\r\n\r\nTime Complexity: O(N)\r\nSpace Complexity: O(1)\r\n\r\n# Approach 1:\r\n```java []\r\nclass Solution {\r\n    public static int max_numbers(int[] arr, int index){\r\n        int max = Integer.MIN_VALUE;\r\n        int ind = 0;\r\n        for (int i = 0; i < arr.length; i++){\r\n            if(arr[i] > max && i != index){\r\n                max = arr[i];\r\n                ind = i;\r\n            }\r\n        }\r\n        return ind;\r\n    }\r\n\r\n    public static int min_numbers(int[] arr, int index){\r\n        int min = Integer.MAX_VALUE;\r\n        int ind = 0;\r\n        for (int i = 0; i < arr.length; i++){\r\n            if(arr[i] < min && i != index){\r\n                min = arr[i];\r\n                ind = i;\r\n            }\r\n        }\r\n        return ind;\r\n    }\r\n    \r\n    public int maxProductDifference(int[] nums) {\r\n        int max1 = max_numbers(nums, -1);\r\n        int max2 = max_numbers(nums, max1);\r\n        int min1 = min_numbers(nums, -1);\r\n        int min2 = min_numbers(nums, min1);\r\n        return (nums[max1]*nums[max2]) - (nums[min1]*nums[min2]);    \r\n    }\r\n}\r\n```\r\n```python []\r\nclass Solution:\r\n    def max_numbers(self, arr: List[int], index: int) -> int:\r\n        max_val = float(\'-inf\')\r\n        ind = 0\r\n        for i in range(len(arr)):\r\n            if arr[i] > max_val and i != index:\r\n                max_val = arr[i]\r\n                ind = i\r\n        return ind\r\n\r\n    def min_numbers(self, arr: List[int], index: int) -> int:\r\n        min_val = float(\'inf\')\r\n        ind = 0\r\n        for i in range(len(arr)):\r\n            if arr[i] < min_val and i != index:\r\n                min_val = arr[i]\r\n                ind = i\r\n        return ind\r\n    \r\n    def maxProductDifference(self, nums: List[int]) -> int:\r\n        max1 = self.max_numbers(nums, -1)\r\n        max2 = self.max_numbers(nums, max1)\r\n        min1 = self.min_numbers(nums, -1)\r\n        min2 = self.min_numbers(nums, min1)\r\n        return (nums[max1] * nums[max2]) - (nums[min1] * nums[min2])\r\n```\r\n```C++ []\r\n\r\nclass Solution {\r\npublic:\r\n    static int max_numbers(const std::vector<int>& arr, int index) {\r\n        int max = INT_MIN;\r\n        int ind = 0;\r\n        for (int i = 0; i < arr.size(); i++) {\r\n            if (arr[i] > max && i != index) {\r\n                max = arr[i];\r\n                ind = i;\r\n            }\r\n        }\r\n        return ind;\r\n    }\r\n\r\n    static int min_numbers(const std::vector<int>& arr, int index) {\r\n        int min = INT_MAX;\r\n        int ind = 0;\r\n        for (int i = 0; i < arr.size(); i++) {\r\n            if (arr[i] < min && i != index) {\r\n                min = arr[i];\r\n                ind = i;\r\n            }\r\n        }\r\n        return ind;\r\n    }\r\n\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int max1 = max_numbers(nums, -1);\r\n        int max2 = max_numbers(nums, max1);\r\n        int min1 = min_numbers(nums, -1);\r\n        int min2 = min_numbers(nums, min1);\r\n        return (nums[max1] * nums[max2]) - (nums[min1] * nums[min2]);\r\n    }\r\n};\r\n\r\n```\r\n\r\n\r\n# Approach 2:\r\n```java []\r\nclass Solution{\r\n    public int maxProductDifference(int[] nums) {\r\n        int max1 = Integer.MIN_VALUE;\r\n        int max2 = Integer.MIN_VALUE;\r\n        int min1 = Integer.MAX_VALUE;\r\n        int min2 = Integer.MAX_VALUE;\r\n        for (int i = 0; i < nums.length; i++) {\r\n            if (nums[i] > max1) {\r\n                max2 = max1;\r\n                max1 = nums[i];\r\n            } else if (nums[i] > max2) {\r\n                max2 = nums[i];\r\n            }\r\n            if (nums[i] < min1) {\r\n                min2 = min1;\r\n                min1 = nums[i];\r\n            } else if (nums[i] < min2) {\r\n                min2 = nums[i];\r\n            }\r\n        }\r\n        return (max1 * max2) - (min1 * min2);\r\n    }\r\n}\r\n```\r\n```python []\r\nclass Solution:\r\n    def maxProductDifference(self, nums: List[int]) -> int:\r\n        max1, max2 = float(\'-inf\'), float(\'-inf\')\r\n        min1, min2 = float(\'inf\'), float(\'inf\')\r\n\r\n        for num in nums:\r\n            if num > max1:\r\n                max2, max1 = max1, num\r\n            elif num > max2:\r\n                max2 = num\r\n\r\n            if num < min1:\r\n                min2, min1 = min1, num\r\n            elif num < min2:\r\n                min2 = num\r\n\r\n        return (max1 * max2) - (min1 * min2)\r\n```\r\n```C++ []\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int max1 = INT_MIN, max2 = INT_MIN;\r\n        int min1 = INT_MAX, min2 = INT_MAX;\r\n\r\n        for (int i = 0; i < nums.size(); ++i) {\r\n            if (nums[i] > max1) {\r\n                max2 = max1;\r\n                max1 = nums[i];\r\n            } else if (nums[i] > max2) {\r\n                max2 = nums[i];\r\n            }\r\n\r\n            if (nums[i] < min1) {\r\n                min2 = min1;\r\n                min1 = nums[i];\r\n            } else if (nums[i] < min2) {\r\n                min2 = nums[i];\r\n            }\r\n        }\r\n\r\n        return (max1 * max2) - (min1 * min2);\r\n    }\r\n};\r\n```\r\n
['Array', 'Python', 'C++', 'Java', 'Python3']