Advertisement
Warmachine28

histogramRainfall.cpp

Oct 25th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. int arr[]={0,1,0,2,3,0,2};
  9. int prefixMaxArr[6];
  10. int suffixMaxArr[6];
  11. int maxPrefix;
  12. int maxSuffix;
  13. int waterLevel = 0;
  14.  
  15. prefixMaxArr[0]=arr[0];
  16. suffixMaxArr[6]=arr[6];
  17. maxPrefix=arr[0];
  18. maxSuffix=arr[6];
  19.  
  20.  
  21.  
  22. for(int i=1;i<=6;i++)
  23. {
  24. if(maxPrefix<arr[i])
  25. {
  26. maxPrefix=arr[i];
  27. prefixMaxArr[i]=maxPrefix;
  28. }
  29. else
  30. {
  31. prefixMaxArr[i]=maxPrefix;
  32. }
  33.  
  34. int j = 6 - i;
  35. if(maxSuffix<arr[j])
  36. {
  37. maxSuffix=arr[j];
  38. suffixMaxArr[j]=maxSuffix;
  39. }
  40. else
  41. {
  42. suffixMaxArr[j]=maxSuffix;
  43. }
  44. }
  45.  
  46.  
  47. for(int i=1;i<6;i++)
  48. {
  49. int h1 = prefixMaxArr[i-1];
  50. int h2 = suffixMaxArr[i+1];
  51. int decid_height = std::min(h1,h2);
  52.  
  53. if(decid_height>arr[i])
  54. {
  55. int temp = decid_height - arr[i];
  56. waterLevel = waterLevel + temp;
  57. }
  58. }
  59.  
  60. cout<<"Water Accumalation in Total: "<<waterLevel<<endl;
  61.  
  62.  
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement