#K35987. Remove Element from Array

    ID: 25653 Type: Default 1000ms 256MiB

Remove Element from Array

Remove Element from Array

You are given an array of integers and an integer val. Your task is to remove all occurrences of val from the array in-place and return the new length of the array after removal. The order of the remaining elements should be preserved.

Formally, given an array \( A \) of length \( n \) and an integer \( val \), remove all elements equal to \( val \) from \( A \) such that if \( k \) elements remain, then the first \( k \) elements of \( A \) contain the elements that are not equal to \( val \). Your output should include the new length \( k \) and the updated array of \( k \) integers.

Note: Since the removal is done in-place, the order of the elements that are not removed should remain the same as their original order.

inputFormat

The input is read from stdin and consists of three lines:

  1. The first line contains a single integer ( n ), representing the number of elements in the array.
  2. The second line contains ( n ) space-separated integers, representing the elements of the array. If ( n = 0 ), this line will be empty.
  3. The third line contains the integer ( val ) which needs to be removed from the array.

outputFormat

The output should be written to stdout and consist of two lines:

  1. The first line contains a single integer ( k ), representing the new length of the array after removing all instances of ( val ).
  2. The second line contains ( k ) space-separated integers representing the modified array. If ( k = 0 ), the second line should be empty.## sample
4
3 2 2 3
3
2

2 2

</p>