Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

&nbsp;
Example 1:
Input: nums = [1,2,3]
Output: 6
Example 2:
Input: nums = [1,2,3,4]
Output: 24
Example 3:
Input: nums = [-1,-2,-3]
Output: -6

&nbsp;
Constraints:


	3 &lt;= nums.length &lt;=&nbsp;104
	-1000 &lt;= nums[i] &lt;= 1000


class Solution:
    def maximumProduct(self, nums: List[int]) -> int:
        # TC = O(NlogN) because sorting the array 
        # SC = O(1); no extra space needed; sorting was done in place.
        
        # sorting the array in descending order
        nums.sort(reverse = True)
        
        # maximum product can only occur for:
        # 1. positive no * positive no * positive no
        # 2. negative no * negative no * positive no
        
        # one negative and two positives and all negatives wont give max product
        # case where all numbers in the array are negative 
        # eg : [-4,-3,-2,-1] is covered in all positives 
        
        return max(nums[0]*nums[1]*nums[2],nums[-1]*nums[-2]*nums[0])
class Solution {
    public int maximumProduct(int[] nums) {
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        for (int n: nums) {
            if (n <= min1) {
                min2 = min1;
                min1 = n;
            } else if (n <= min2) { // n lies between min1 and min2
                min2 = n;
            }
            if (n >= max1) { // n is greater than max1, max2 and max3
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if (n >= max2) { // n lies betweeen max1 and max2
                max3 = max2;
                max2 = n;
            } else if (n >= max3) { // n lies betwen max2 and max3
                max3 = n;
            }
        }
        return Math.max(min1 * min2 * max1, max1 * max2 * max3);
    }
}
class Solution {
public:
    int maximumProduct(vector<int>& nums) {

        int min1 = INT_MAX , min2 = INT_MAX; // Both have the maximum value that is +infinte
        int max1 = INT_MIN, max2 = INT_MIN , max3 = INT_MIN; //All these have the minimum value that is -infinte

        //now finding all these value above declared.
        for(int i = 0 ; i < nums.size() ; i++)
        {
            if(nums[i] > max1 )
            {
                max3 = max2 ;
                max2 = max1;
                max1 = nums[i];

            }
            else if(nums[i] > max2)
            {
                max3 = max2 ;
                max2 = nums[i] ;

            }
            else if(nums[i] > max3)
            {
                max3 = nums[i] ;

            }

            if(nums[i] < min1)
            {
                min2 = min1 ;
                min1 = nums[i] ;

            }
            else if(nums[i] < min2)
            {
                min2 = nums[i];

            }
        }
        return max(min1*min2*max1 , max1*max2*max3);
    }
};
var maximumProduct = function(nums) {
    nums.sort((a, b) => a-b)
    
    var lastNumber = nums.length - 1
    var midNumber = nums.length - 2
    var firstNumber = nums.length - 3
    var total = nums[lastNumber] * nums[midNumber] * nums[firstNumber]
    return total
    
};
Maximum Product of Three Numbers