✅✅Two Java Solution || Easy Explanation 🔥 🔥
Approach 1\nThe code starts by sorting the input array, "nums," in ascending order using the Arrays.sort() method. Sorting the array allows us to consider diffe
# Approach 1\nThe code starts by sorting the input array, "nums," in ascending order using the **Arrays.sort()** method. Sorting the array allows us to consider different cases and calculate the maximum product.\n\nThe next step involves calculating two potential maximum products:\n\n1. "case1" represents the product of the two smallest numbers (nums[0] and nums[1]) in the sorted array, multiplied by the largest number (nums[nums.length-1]). You may think why do I use the first two number. Just think, if there are negative numbers and if I multiply two negative numbers then the resultant will be positive. \n\n2. "case2" represents the product of the three largest numbers in the sorted array (nums[nums.length-1], nums[nums.length-2], and nums[nums.length-3]).\n\nAfter calculating both cases, the code determines the maximum value between "case1" and "case2" using the Integer.max() method. The maximum value is then stored in the variable "maxProduct."\n\nFinally, the "maxProduct" value is returned as the result of the method.\n\nIn summary, this code finds the maximum product of three numbers in an input array by sorting the array, considering two different cases, and selecting the maximum value between them.\n```\nclass Solution {\n    public int maximumProduct(int[] nums) {\n        Arrays.sort(nums);\n        int case1 = nums[0]*nums[1]*nums[nums.length-1];\n        int case2 = nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3];\n\n        int maxProduct = Integer.max(case1, case2);\n        return maxProduct;\n    }\n}\n```\n\n---\n\n# Approach 2\nLet\'s break down the second approach step by step:\n\n1. We have a class named `Solution`, which contains a method `maximumProduct` that takes an integer array `nums` as input and returns an integer.\n\n2. Inside the method, we declare several variables: `min1`, `min2`, `max1`, `max2`, and `max3`. These variables are used to keep track of the minimum and maximum values encountered in the array.\n\n3. We initialize `min1`, `min2`, `max1`, `max2`, and `max3` with the maximum and minimum possible integer values, respectively. This ensures that the initial values will be overwritten by the actual values in the array.\n\n4. We start a loop that iterates over each element in the `nums` array.\n\n5. Inside the loop, we compare the current element with the current maximum values (`max1`, `max2`, and `max3`) to determine if any of them should be updated. If the current element is greater than `max1`, we update `max3`, `max2`, and `max1` accordingly. If it is greater than `max2` but not greater than `max1`, we update `max3` and `max2`. If it is greater than `max3` but not greater than `max2` or `max1`, we update only `max3`.\n\n6. Similarly, we compare the current element with the current minimum values (`min1` and `min2`) to determine if any of them should be updated. If the current element is smaller than `min1`, we update `min2` and `min1` accordingly. If it is smaller than `min2` but not smaller than `min1`, we update only `min2`.\n\n7. After the loop completes, we calculate the maximum product by taking the maximum of two possible products: `max1 * max2 * max3` and `min1 * min2 * max1`. This is done using the `Math.max` function.\n\n8. Finally, we return the maximum product as the result.\n\n```\nclass Solution {\n    public int maximumProduct(int[] nums) {\n        int min1, min2, max1, max2, max3;\n        min1 = min2 = Integer.MAX_VALUE;\n        max1 = max2 = max3 = Integer.MIN_VALUE;\n        for(int i = 0; i < nums.length; i++){\n            if(max1 < nums[i]){\n                max3 = max2;\n                max2 = max1;\n                max1 = nums[i];\n            }else if(max2 < nums[i]){\n                max3 = max2;\n                max2 = nums[i];\n            }else if(max3 < nums[i]){\n                max3 = nums[i];\n            }\n            \n\n            if(min1 > nums[i]){\n                min2 = min1;\n                min1 = nums[i];\n            }else if(min2 > nums[i]){\n                min2 = nums[i];\n            }\n            \n        }\n        \n        return Math.max(max1 * max2 * max3 , min1 * min2 * max1);\n    }\n}\n```\n\n**Please upvote if you like the solution.\nHappy Coding!** \uD83D\uDE0A
['Array', 'Math', 'Sorting', 'Java']