#K58987. Remove Element

    ID: 30764 Type: Default 1000ms 256MiB

Remove Element

Remove Element

Given an array of integers nums of length nn 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 kk elements of nums should hold the final result, where kk is the new length of the array. You do not need to consider elements beyond the first kk 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:

  1. The first line contains an integer nn, the number of elements in the array.
  2. The second line contains nn space-separated integers representing the array nums.
  3. The third line contains an integer val to be removed from the array.

outputFormat

Output the result to standard output (stdout) in two lines:

  1. The first line should be an integer kk, the new length of the array after removal.
  2. The second line should contain the first kk elements of the modified array, separated by spaces. If k=0k = 0, the second line can be empty.## sample
4
3 2 2 3
3
2

2 2

</p>