#K58987. Remove Element
Remove Element
Remove Element
Given an array of integers nums
of length and an integer val
, remove all occurrences of val
from nums
in-place. The relative order of the remaining elements should be maintained. After removal, the first elements of nums
should hold the final result, where is the new length of the array. You do not need to consider elements beyond the first elements. Use a two-pointer technique to achieve an optimal solution.
inputFormat
The input is given via standard input (stdin) and consists of three lines:
- The first line contains an integer , the number of elements in the array.
- The second line contains space-separated integers representing the array
nums
. - The third line contains an integer
val
to be removed from the array.
outputFormat
Output the result to standard output (stdout) in two lines:
- The first line should be an integer , the new length of the array after removal.
- The second line should contain the first elements of the modified array, separated by spaces. If , the second line can be empty.## sample
4
3 2 2 3
3
2
2 2
</p>