Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
2
Indexable
Never
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        
        int prod = 1;
        int maxx = INT_MIN;
        
        int count = 0;
        int count2 = 0;
        for (int i=0; i<nums.size(); i++){
            count++; 
        }
        count2 = count;
        for (int i=0; i<nums.size(); i++){
            
            if(nums[i]<0){
                count--;
            }
            if(prod<0 && count==0)
            {
                prod =1;
            }
            else if(prod>0 && count==0 && nums[i]<0  || nums[i]==0){
                prod = 1;
            }
            else {
                prod *= nums[i];    
            }
            
            maxx = max(maxx,prod);
        }
        
        
        prod = 1;
        for (int i=nums.size()-1; i>=0; i--){
            
            if(nums[i]<0){
                count--;
            }
            if(prod<0 && count==0)
            {
                prod =1;
            }
            else if(prod>0 && count==0 && nums[i]<0  || nums[i]==0){
                prod = 1;
            }
            else {t
                prod *= nums[i];    
            }
            
            maxx = max(maxx,prod);
        }
        
        
        return maxx;
    }
};