I am having trouble understanding the concept

During dry run i am getting wrong answer it would be beneficial if you could share the relevant code with me