Given an array of integers and a target value, move all instances of that target value to the end end of the array. The order of the resulting array does not matter in this question.
left
and right
.left
pointer is intialized to the 0th
index. The right
pointer is initialized to the last index of the array.left
pointer is to check if the value at the left
index is EQUAL to the target value
.left < right
. If arr[left]
is equal to the target value - This means we found a target value that needs to be at the end of the array.right
pointer initialized to the end of the array, we swal the arr[left]
and arr[right]
values. Essentially taking the target value at the end. After swapping, we decrement
the right pointer, because we might encounter more such target values that need to be at the end.left
value is not equal to the target value
, we simply increment the left pointer.O(n)
Since we are iterating the array only once.