Test cases fail

#include
using namespace std;
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;

}
int main() {
int arr[1000];
int v;
int n;
int pb;
cin>>n;
for(v=0;v<n;v++)
{
cin>>arr[v];
}
pb=maxWater_optimized(arr,n);
cout<<pb;
return 0;
}

@chandeep please correct the array size as N can be upto 10^6

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.