Explained In Two Approaches
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is a simple and straightforwad approach.\nBy seeing th test cases we can identify 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a simple and straightforwad approach.\nBy seeing th test cases we can identify that the max difference can be achived by simply calculating the difference of products between (first_max * second_max)-(first_min*second_min)\n# Approach1\n<!-- Describe your approach to solving the problem. -->\nBy Soring the array in ascending order \nfirst_max=a[n-1]\nsecond_max=a[n-2]\n\nfirst_min=a[0]\nsecond_min=a[1]\n\nsimply we find product of these two pairs and return their difference\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n*log(n)) \nfor sorting array\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1) -> No extra space was used\n\n# Code\n```\nclass Solution {\n    public int maxProductDifference(int[] a) {\n        Arrays.sort(a);\n        int n=a.length;\n        return ((a[n-1]*a[n-2])-(a[0]*a[1]));\n\n    }\n}\n```\n\n# Approach2\nInstead of sorting the array lets find the all the pairs using linear search\ninitialize \nfirst_max=0\nsecond_max=0 //as 1 <= a[i] <= 104 -> the least val possible is 1\n\nfirst_min=INT_MAX\nsecond_min=INT_MAX\n\nIterate a loop from 0 -> n-1\nif(a[i]>first_max) then update the second_max=first_max and first_max=a[i]\nif(a[i]>second_max && a[i]<first_max) then just update the second_max\nto a[i]\n\nSimilarly do this to find first_min & second_min\n\nat last find product of these two pairs and return their difference\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) \nfor linear search to find the pairs\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1) -> No extra space was used\n\n# code\n```\nclass Solution {\n    public int maxProductDifference(int[] a) {\n        int fmax=0,smax=0;\n        int fmin=Integer.MAX_VALUE,smin=Integer.MIN_VALUE;\n        for(int x:a){\n            if(x<fmin){\n                smin=fmin;\n                fmin=x;\n            }\n            else if(x<smin)\n            {\n                smin=x;\n            }\n            if(x>fmax){\n                smax=fmax;\n                fmax=x;\n            }\n            else if(x>smax)\n                smax=x;\n        }\n        return ((fmax*smax)-(fmin*smin));\n\n    }\n}\n```\n\n> Thank You for Viewing \uD83D\uDE0A\n
['Java']