#K5021. Move Target Elements to the End
Move Target Elements to the End
Move Target Elements to the End
You are given an array of integers and a target integer. Your task is to move all occurrences of the target integer to the end of the array while preserving the relative order of the non-target elements. This operation must be performed in-place with minimal extra space.
Formally, given an array \( nums \) and an integer \( target \), rearrange the elements of \( nums \) so that all elements not equal to \( target \) maintain their order and all occurrences of \( target \) are shifted to the end.
Example:
Input: n = 5, nums = [0, 1, 0, 3, 12], target = 0 Output: 1 3 12 0 0
inputFormat
The input is read from stdin and consists of three parts:
- An integer \( n \) representing the number of elements in the array.
- A line containing \( n \) space-separated integers representing the array \( nums \).
- An integer representing the target value \( target \).
outputFormat
Output to stdout the modified array in one line. The elements of the array should be printed as space-separated integers.
## sample5
0 1 0 3 12
0
1 3 12 0 0