left
and right
to represent the window's boundaries.sum
to store the total number of fruits within the window.right
pointer, adding the number of fruits at the current position to sum
.left
to right
exceeds k
steps, move the left
pointer to the right, subtracting the number of fruits at the left
position from sum
.maxi
if the current sum
is greater.right
pointer reaches the end of the array.maxi
as the maximum total number of fruits that can be harvested.