|
U
Pro30. Remove All Occurrences of Value in Place
Easy
Problem Description
Remove all occurrences of integer val from an array of N integers in-place. Preserve the relative order of the remaining elements.
Input Format
- •Line 1: Two space-separated integers N and val (1≤N≤105).
- •Line 2: N space-separated integers A1,…,AN.
Output Format
- •Line 1: An integer K, the new logical size.
- •Line 2: The K remaining space-separated integers, or
EMPTYif K=0.
Example 1:
Input:
6 3 3 2 2 3 4 3
Output:
3 2 2 4
Explanation: Removing all 3s leaves 2, 2, 4 with new size 3.
Example 2:
Input:
4 1 1 1 1 1
Output:
0 EMPTY
Explanation: All elements equal 1, leaving size 0 and EMPTY.
Example 3:
Input:
3 5 1 2 3
Output:
3 1 2 3
Explanation: Value 5 does not appear in array; array unchanged.
Constraints:
Standard integer bounds: 32-bit signed integer or input range [1, 10^5].Time complexity target: O(N) or O(N log N) where applicable.Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit