Problem statement
You are given an array of whole numbers and a number k. The task has two parts. First, reverse the array: the first value and the last value trade places, then the next pair in, and so on. Second, rotate the array right by k steps: every value moves k slots to the right, and values that fall off the end reappear at the front.
Both tasks must be done in place. No second array may be created, and the same array that was given must hold the answer.
Examples
Inputa = [1, 2, 3, 4, 5]
Output[5, 4, 3, 2, 1]
After the reverse, the middle value never moves.
Inputa = [1, 2, 3, 4, 5], k = 2
Output[4, 5, 1, 2, 3]
The last two values wrap around to the front.
The walkthrough is part of Premium
The statement and the examples above stay free. The full how-to-solve walkthrough, the cost notes in plain words and the interactive step-by-step visualizer unlock with a Premium plan.
See pricing