Advertisement
l_garg

Untitled

Jul 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 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, imax=max_prod, imin=max_prod; i<n; i++){
  5.             // cout<<"i="<<i<<endl;
  6.             // cout<<"imax="<<imax<<endl;
  7.             // cout<<"imin="<<imin<<endl;
  8.             if(nums[i]<0){
  9.                 swap(imax, imin);
  10.             }
  11.             imax=max(nums[i]*imax, nums[i]);
  12.             imin=min(nums[i]*imin, nums[i]);
  13.             max_prod=max(max_prod, imax);
  14.         }
  15.         return max_prod;
  16.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement