Remove Element
Practice this coding question on Coding Gurukul.
Problem statement
A stock terminal stores product codes in a fixed array. A recalled code must be removed before the remaining entries are displayed. Your task Compact all codes different from val into the beginning of the same array and report the retained prefix. Important details Use O(n) time and O(1) auxiliary space for compaction. For this console version preserve the original order, so every valid submission prints the same prefix. Ignore array cells after the retained prefix. An empty input array or removal of every value produces an empty prefix. Worked example For codes 4 2 4 7 2 and val = 4, move 2, 7 and 2 into the first three positions. Print 3 followed by 2 7 2.
Input format
First line: n and val. Next line: n integer codes; omit it when n = 0.
Output format
Print k on the first line. On the second line print the k retained codes, or EMPTY if k = 0.
Constraints
0 <= n <= 1000; -1000000000 <= codes[i], val <= 1000000000.
Sample input
5 4 4 2 4 7 2
Sample output
3 2 7 2
Sign in to run or submit a solution in C++, Java or Python.