fork(8) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int arr[100005];
  4. int lsl[100005];
  5. int lgr[100005];
  6. stack<int> s;
  7. int main()
  8. {
  9. int n;
  10. scanf("%d",&n);
  11. for(int i=0;i<n;i++)
  12. scanf("%d",&arr[i]);
  13. int largest=-1;
  14. for(int i=n-1;i>=0;i--)
  15. {
  16. if((largest==-1)||(largest!=-1&&arr[i]<arr[largest]))
  17. lgr[i]=largest;
  18. else
  19. lgr[i]=-1;
  20. if((largest==-1)||(largest!=-1&&arr[i]>arr[largest]))
  21. largest=i;
  22. }
  23. for(int i=0;i<n;i++)
  24. {
  25. if(lgr[i]==-1)
  26. {
  27. lsl[i]=-1;
  28. continue;
  29. }
  30. int max=-1;
  31. while(!s.empty()&&arr[s.top()]<arr[i])
  32. max=s.top(),s.pop();
  33. lsl[i]=max;
  34. s.push(i);
  35. }
  36. int ma=-1;
  37. int maxi=-1,maxj=-1,maxk=-1;
  38. for(int i=1;i<n-1;i++)
  39. {
  40. if(lsl[i]!=-1&&lgr[i]!=-1)
  41. {
  42. if(ma<arr[lsl[i]]*arr[i]*arr[lgr[i]])
  43. {
  44. maxi=lsl[i];
  45. maxj=i;
  46. maxk=lgr[i];
  47. ma=arr[lsl[i]]*arr[i]*arr[lgr[i]];
  48. }
  49. }
  50. }
  51. if(ma!=-1)
  52. printf("The numbers are %d %d %d with product value %d\n",arr[maxi],arr[maxj],arr[maxk],ma);
  53. else
  54. printf("No such sequence exist\n");
  55. return 0;
  56. }
Success #stdin #stdout 0s 4652KB
stdin
5
1 5 10 8 9
stdout
The numbers are 5 8 9 with product value 360