Skip to content
BytePatterns

Push Target Values Back

EasyArrays#two-pointers#write-pointer~15m

Problem

A playlist editor wants every copy of one song id moved to the end of the queue without shuffling anything else. Given a list nums and a value v, rearrange the list in place so that the elements different from v keep their original relative order at the front and every copy of v sits at the back. Return the same list object, and use only a constant amount of extra memory.

Examples

Input:  nums = [4, 0, 7, 0, 2], v = 0
Output: [4, 7, 2, 0, 0]
Why:    4, 7 and 2 stay in the order they arrived
Input:  nums = [3, 1, 3, 3, 5], v = 3
Output: [1, 5, 3, 3, 3]
Why:    three copies of 3 collect at the back
Input:  nums = [], v = 9
Output: []
Why:    edge case, there is nothing to move

Hints

0 / 3

Stuck on the idea rather than the code? Move Zeroes covers it.