 Maximum water container problem? | Sololearn: Learn to code for FREE!

0

# Maximum water container problem?

Given an array of heights.[6,3,4,5] Maximum water container will be minimum of two sides of container multiplied by the distance between them. For the given array output will be: min(6,5)*3 = 15.

+2

Where is your attempt?

0

https://code.sololearn.com/ce4lLtwNV8UE/?ref=app

0

Your while loop infinite loop there. But why do you need a loops there? Two sides of containers are first (0th) value and last (array length-1 th) value and distance is arraylength -1 so its enough to code like : #include <iostream> using namespace std; int getArea(int a[],int size){ int maxArea = 0,i=0,j= size-1; //while(j>i){ int currArea = min(a[i],a[j])*(j-i); maxArea = max(currArea ,maxArea); // while(a[i] < a[i+1] && i<j){i++;} // while(a[j] < a[j-1] && j>i){j--;} //according to your data 5<2 and 6<3 never true and outer loop while(j>i) always true and not modified I, j so its infinite.. // } return maxArea ; } int main() { int a[] = {5,2,3,6}; cout << getArea(a,4); return 0; } you can write it simply : int main() { int a[] = {5,2,3,6}; int length = sizeof(a)/sizeof(*a); //array length cout << min(a,a[length-1])*(length-1); return 0; } hope it helps..

0

G'day Hritik congratulations on getting the code to do what you wanted. Do you love the way C++ can do things so elegantly? Would you do a bit of housekeeping, edit your original post to start with [Solved]? Have you found the Q&A search bar (it is the magnifying glass icon at the top of the discuss screen)? Did you know you can link other Q&A posts or SoloLearn code bits (use the plus icon)? https://www.sololearn.com/discuss/1316935/?ref=app https://www.sololearn.com/discuss/333866/?ref=app