In this problem if we find the highest bar on left and right side of the current bar, than the amount of water current bar can hold will be ={min(left_max,right_max)-height[cur]}

It can be done by using two pointers.

int TrappingWater(int height[],int n)

{

int left_max=0,right_max=0,ans=0;

int left=0,right=n-1;

while(left<=right)

{

if(height$<$height[right])

{

if(height>=left_max) left_max=height;

else ans+=left_max-height;

++left;

}

else

{

if(height[right]>=right_max) right_max = height[right];

else ans+=right_max - height[right];

–right;

}

}

return ans;

}