Given an integer array representing the heights of N buildings, the task is to delete N-2 buildings such that the water that can be trapped between the remaining two building is maximum.
Maximum Water Between Two Buildings
class Solution { //Function to return the maximum water that can be stored. static int maxWater(int height[], int n) { //Your code here int i=0; int j=n-1; int maxwater=0; while(i<j){ maxwater=Math.max(maxwater, Math.min(height[i],height[j])*(j-i-1)); if(height[i]<height[j]) i++; else j--; } return maxwater; } }
Disclaimer: This problem (Maximum Water Between Two Buildings) is originally created by GeeksforGeeks. Codesagar only provides a solution for it. This solution is only for Educational and learning purposes.