Advertisement
otot957

Untitled

Feb 13th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. /*
  4. multiplying 3 numbers from an array to get highest product possible
  5.  
  6. */
  7. int func(vector<int> vect){
  8.   int min1= INT_MAX, min2=min1;
  9.   int max1= INT_MIN, max2= INT_MIN, max3= INT_MIN;
  10.   for(int i=0; i<vect.size(); i++){
  11.     if(vect[i] >= max1){
  12.       max3= max2;
  13.       max2= max1;
  14.       max1= vect[i];
  15.     }
  16.     else if(vect[i] >= max2){
  17.       max3= max2;
  18.       max2= vect[i];
  19.     }
  20.     else if(vect[i] >= max3)
  21.       max3= vect[i];
  22.     if(vect[i] <= min1){
  23.       min2= min1;
  24.       min1= vect[i];
  25.     }
  26.     else if(vect[i] <= min2)
  27.       min2= vect[i];
  28.   }
  29.   return max(max1*max2*max3, min1*min2*max1);
  30.  
  31. }
  32.  
  33.  
  34. int main(){
  35.   vector<int> vect= {1,3,4,-10,-10,10};
  36.  cout<<func(vect);
  37.   return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement