If your logic is right but you are getting WA in some cases. Generate the largest case and check what happens
I have used some simple names for all arrays and vectors so that it is easy for you guys to understand.
This video helped me. To know why this works think and work out through some examples by hand with this approach.