JavaScript O(n) time, O(1) space solution without sorting
We don\'t need to completly sort the input array - it is enough to keep track of the 2 smallest / largest numbers:\n\n\nvar maxProductDifference = function(nums
We don\'t need to completly sort the input array - it is enough to keep track of the 2 smallest / largest numbers:\n\n```\nvar maxProductDifference = function(nums) {\n    \n    let min = 1e4, prevMin = min;\n    let max = 0, prevMax = 0;\n    \n    for (const n of nums) {\n        \n        if (n > max) {\n            prevMax = max;\n            max = n;\n        } else if (n > prevMax) {\n            prevMax = n;\n        }\n        \n        if (n < min) {\n            prevMin = min;\n            min = n;\n        } else if (n < prevMin) {\n            prevMin = n;\n        }\n    }\n    \n    return max*prevMax - min*prevMin;\n};\n```
['JavaScript']