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.
leftpointer is intialized to the
rightpointer is initialized to the last index of the array.
leftpointer is to check if the value at the
leftindex is EQUAL to the
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.
rightpointer initialized to the end of the array, we swal the
arr[right]values. Essentially taking the target value at the end. After swapping, we
decrementthe right pointer, because we might encounter more such target values that need to be at the end.
leftvalue is not equal to the
target value, we simply increment the left pointer.
O(n)Since we are iterating the array only once.