Advertisement
l_garg

Untitled

Jul 15th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. int maxProduct(vector<int>& nums) {
  2.         int n=nums.size();
  3.         int max_prod=nums[0];
  4.         for(int i=1; i<n; i++){
  5.             int imax=max_prod;
  6.             int imin=max_prod;
  7.             if(nums[i]<0){
  8.                 swap(imax, imin);
  9.             }
  10.             imax=max(nums[i]*imax, nums[i]);
  11.             imin=min(nums[i]*imin, nums[i]);
  12.             max_prod=max(max_prod, imax);
  13.         }
  14.         return max_prod;
  15.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement