Shift Right and Fill
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
0 <= len(nums) <= 100-1000 <= nums[i] <= 10000 <= k <= 100-1000 <= fill <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Shuffle the ArrayBeginner
- Sort by ParityBeginner
- Trend SignsBeginner
- Alternate the SignsEasy
- Drop the Lowest ScoresEasy
- Fair CutsEasy
- Final Tank LevelEasy
- Find Closest Number to ZeroEasy