Best Approach
Use the two pointer approach. Loop from index 0 to the end of the given array. If a wall greater than or equal to the previous wall is encountered then make note of the index of that wall in a var called previndex. Keep adding previous wall’s height minus the current (ith) wall to the variable water. Have a temporary variable that stores the same value as water. If no wall greater than or equal to the previous wall is found then quit. If previndex < size of the input array then subtract the temp variable from water, and loop from end of the input array to prev_index and find a wall greater than or equal to the previous wall (in this case, the last wall from backwards).
C++ Code
int maxWater_optimized(int arr[], int n)
{
int water = 0; // To store the final ans
int left_max = 0; // Which stores the current max height of the left side
int right_max = 0; // Which stores the current max height of the right side
int lo = 0; // Counter to traverse from the left_side
int hi = n - 1; // Counter to traverse from the right_side
while (lo <= hi)
{
if (arr[lo] < arr[hi])
{
if (arr[lo] > left_max)
{
left_max = arr[lo]; // Updating left_max
}
else
{
water += left_max - arr[lo]; // Calculating the ans
}
lo++;
}
else
{
if (arr[hi] > right_max)
{
right_max = arr[hi]; // Updating right_max
}
else
{
water += right_max - arr[hi]; // Calculating the ans
}
hi--;
}
}
return water;
}
I am finding this logic great but can you explain whether this can be used as a general approach in some kind of problems or is this just question specific.