1class Solution {2public int minimumRefill(int[] p, int ca, int cb) {34int refill = 0,5oca = ca,6ocb = cb; // let save our orginal capacity , needed to refill can again7int i = 0, j = p.length - 1; // starting both end89while (i <= j) {1011if (i == j) { // mean both at same position12if (ca >= cb) {13if (p[i] > ca) {14refill++;15}16} else {17if (p[j] > cb) {18refill++;19}20}21// no more plant left for watering so break loop22break;23}2425// first check if they have sufficient amount of water26// if not then refill it with orginal capacity2728if (p[i] > ca) {29refill++;30ca = oca;31}32if (p[j] > cb) {33refill++;34cb = ocb;35}3637// decrease consumed water38ca -= p[i];39cb -= p[j];4041// move both42i++;43j--;44}45return refill;46}47}