Advertisement
Warmachine28

maximizeTheExpression

Oct 22nd, 2021
54
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. int arr[7]={10,15,19,-97,38,36,51};
  9. int p = -5;
  10. int q = 12;
  11. int r = 10;
  12. int prefixMax[7];
  13. int suffixMax[7];
  14. int maxPrefix = p*arr[0];
  15. int maxSuffix = r*arr[6];
  16. int result=INT16_MIN;
  17.  
  18. prefixMax[0]=maxPrefix;
  19. suffixMax[6]=maxSuffix;
  20. for(int i=1;i<=6;i++)
  21. {
  22. if(maxPrefix<p*arr[i])
  23. {
  24. maxPrefix=p*arr[i];
  25. prefixMax[i]=maxPrefix;
  26. }
  27. else
  28. {
  29. prefixMax[i]=maxPrefix;
  30. }
  31.  
  32. }
  33. for(int i=5;i>=0;i--)
  34. {
  35. if(maxSuffix<r*arr[i])
  36. {
  37. maxPrefix=r*arr[i];
  38. suffixMax[i]=maxSuffix;
  39. }
  40. else
  41. {
  42. suffixMax[i]=maxSuffix;
  43. }
  44.  
  45. }
  46.  
  47. for(int i=1;i<6;i++)
  48. {
  49. if(result<prefixMax[i-1]+arr[i]+suffixMax[i+1])
  50. {
  51. result = prefixMax[i-1]+arr[i]+suffixMax[i+1];
  52. }
  53.  
  54. }
  55.  
  56. cout<<result<<endl;
  57.  
  58. return 0;
  59. }
Advertisement
RAW Paste Data Copied
Advertisement