Video link: [Link]
be/fCbECIotNz4
Visual aids used in the exposition
Original array:
nums = [4, 9, 4, 9, 5, 5, 4, 9, 5]
Create auxiliary boolean array to check if the
number has been used (rearranged).
nums = [4, 9, 4, 9, 5, 5, 4, 9, 5]
used = [x, x, x, x, x, x, x, x, x]
Search in the array set by parameter.
if the number in that position in the array is a 4
:
nums = [4, 9, 4, 9, 5, 5, 4, 9, 5]
used = [x, x, x, x, x, x, x, x, x]
Look for an unused five in the array:
nums = [4, 9, 4, 9, 5
, 5, 4, 9, 5]
used = [x, x, x, x, x, x, x, x, x]
Save the position:
auxIndex = the index of the 5 we’re using (index 4, in this case)
nums = [4, 9, 4, 9, 5
, 5, 4, 9, 5]
used = [x, x, x, x, x, x, x, x, x]
*Stops navigating the array*
At the saved position (5’s index), replace the 5 with
the value in front of the 4.
nums = [4, 9, 4, 9, 9 (u sed to be 5), 5, 4, 9, 5]
used = [x, x, x, x, √, x, x, x, x]
Replace the value in front of the 4 for a 5 and mark
that number as used.
nums = [4, 5, 4, 9, 9, 5, 4, 9, 5]
used = [x, √, x, x, √, x, x, x, x]