spider68

maximum product subarray

Jun 16th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int i,j,t,n;
  6.     int mod=1000000007;
  7.     cin>>t;
  8.     while(t--)
  9.     {
  10.         cin>>n;
  11.         int a[n];
  12.         for(i=0;i<n;i++)cin>>a[i];
  13.         int mx=a[0],mn=a[0],r=a[0];
  14.         for(i=1;i<n;i++)
  15.         {
  16.             if(a[i]<0)swap(mx,mn);
  17.             mx=max(a[i],a[i]*mx);
  18.             mn=min(a[i],mn*a[i]);
  19.             mx%=mod;
  20.             mn%=mod;
  21.             r=max(r,mx);
  22.         }
  23.         cout<<r<<endl;
  24.     }
  25.     return 0;
  26. }
Add Comment
Please, Sign In to add comment