✅Easiest 3 Step Cpp / Java / Py / C / JS Solution | Beginner Level 🏆 | With Explanation
\n### Explanation:\n1.Sort the Array:\nThe first step is to sort the array in ascending order. This helps in easily identifying the smallest and largest element
\n### **Explanation:**\n**1.Sort the Array:**\nThe first step is to sort the array in ascending order. This helps in easily identifying the smallest and largest elements.\nIdentify the Two Smallest and Two Largest Elements:\n\nAfter sorting, the first two elements of the array are the two smallest elements.\nThe last two elements of the array are the two largest elements.\n\n**2.Calculate the Products:**\nCalculate the product of the two smallest elements.\nCalculate the product of the two largest elements.\n\n**3.Calculate the Difference:**\nSubtract the product of the two smallest elements from the product of the two largest elements to get the desired result.\n\n**4.Return the Result:**\nReturn the calculated difference.\n\n---------------------------------------------------------------\n\n### C++\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        \n        sort(nums.begin(),nums.end());\n\t\t//first sort array Using STL\n        \n        int n=nums.size();\n        \n        int p1=nums[0]*nums[1]; //store product of 2 small numbers\n        int p2=nums[n-1]*nums[n-2];  //store product of two large numbers\n        \n        int res=p2-p1;\n        \n        return res;\n    }\n};\n```\n\n### Java\n```\nclass Solution {\n    public int maxProductDifference(int[] nums) {\n        \n        Arrays.sort(nums);\n        \n        int n=nums.length;\n        \n        int p1=nums[n-2]*nums[n-1];\n        int p2=nums[0]*nums[1];\n        \n        int res=p1-p2;\n        \n        return res;\n    }\n}\n```\n\n### Python3\n```\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        nums.sort()  # Sort the array\n        \n        n = len(nums)\n        \n        p1 = nums[0] * nums[1]\n        p2 = nums[n - 1] * nums[n - 2]\n        \n        res = p2 - p1\n        \n        return res\n```\n\n### C\n```\n// Comparator function for qsort\nint compare(const void* a, const void* b) {\n    return (*(int*)a - *(int*)b);\n}\n\nint maxProductDifference(int* nums, int numsSize) {\n    // Sort the array\n    qsort(nums, numsSize, sizeof(int), compare);\n    \n    // Calculate the product of the two smallest numbers\n    int p1 = nums[0] * nums[1];\n    // Calculate the product of the two largest numbers\n    int p2 = nums[numsSize - 1] * nums[numsSize - 2];\n    \n    // Calculate the result\n    int res = p2 - p1;\n    \n    return res;\n}\n```\n\n### JavaScript\n\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxProductDifference = function(nums) {\n    // Sort the array\n    nums.sort((a, b) => a - b);\n    \n    // Calculate the product of the two smallest numbers\n    let p1 = nums[0] * nums[1];\n    // Calculate the product of the two largest numbers\n    let p2 = nums[nums.length - 1] * nums[nums.length - 2];\n    \n    // Calculate the result\n    let res = p2 - p1;\n    \n    return res;\n};\n\n```\n\n![image](https://assets.leetcode.com/users/images/ad294515-01e3-4704-a668-9bcc5b0e822c_1717851160.1975598.gif)\n\n
['Array', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']