Close the gap from both ends02.01
Closing the gap moves both ends inward in one step: L += 1 and R -= 1. The unprocessed slice loses one index on each side.
After a close step the new indices are the old L+1 and R-1. Everything outside the new [L, R] is finished. Odd n meets. Even n still has a two-cell slice, or crosses if you started on two cells.
Same skill, three traces. Read all three before you try it.
01three - given
a = [4, 1, 9]- walk
start
L=0,R=2L+=1,R-=1- result
L=1,R=1
02four - given
a = [4, 1, 9, 1]- walk
start
L=0,R=3L+=1,R-=1- result
L=1,R=2
03one - given
a = [7]- walk
start
L=0,R=0already no gap; do not close
- result
L=0,R=0
closeGap(a).a is an array of integers. L is the left index, start 0. R is the right index, start n-1.Do
L += 1 and R -= 1 once. Print the new L and R. Return nothing.On
[4, 1, 9] print L=1, R=1. On [4, 1, 9, 1] print L=1, R=2. On [4, 1] print L=1, R=0.constraintsa has length ≥ 2. Start with L = 0 and R = n-1.
- Container With Most Water0/3 skills ready · two pointers · mediumopen
- Container With Most Water0/3 skills ready · two pointers · mediumopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.