FunctionRacerProblemsRace

Shift Right and Fill

BeginnerArraysPython, JavaScript, TypeScript, Java, C++

Shift the items of nums to the right by k places. The k items that fall off the right end are discarded, and the k places that open up at the front are filled with the value fill.

Return the new list, which has the same length as nums. With k = 0 the list is unchanged, and when k is at least len(nums) every item becomes fill. Do not change nums.

Example 1

nums [1,2,3,4,5]
k 2
fill 0
returns [0,0,1,2,3]

Everything moves two places right: 4 and 5 fall off, and two zeros fill the front.

Example 2

nums [7,8,9]
k 0
fill -1
returns [7,8,9]

A shift of 0 changes nothing.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems