Rain water harvesting problem

i made a code for the problem but i don’t know where it is lacking https://ideone.com/0s4391

Tushar, you are using wrong approach for calculating the rightmost and leftmost array, try to use this approach :
leftmost[i]=max(ar[i],leftmost[i-1]);
rightmost[i]=max(ar[i],rightmost[i+1]);
and then calculate the value using this