Kotlinlearncs.online LogoJava
Return to List

Solve: Rotate Array

Created By: Justin Maier
/ Version: 2024.9.0

Write a function rotateArray that takes two arguments: first, an int array, and second, an int we will call steps, representing the number of steps to rotate the array by. The function should return a new array with the same values as the original argument, but with the elements rotated by steps spaces. You should not modify the original array.

If steps is positive, the returned array should be rotated to the right by that many places. For example, if the array is {1, 2, 3, 4, 5} and steps is 2, you should return the array {4, 5, 1, 2, 3}.

If steps is negative, the returned array should be a rotation to the left of the original. For example, if the original array is {1, 2, 3, 4, 5} and steps is -2, you should return the array {3, 4, 5, 1, 2}.

If steps is zero, the returned array should match the original.

Additionally, you should use an assert statement to check that the array is not empty.

Related Lessons

Stuck? You may find these lessons helpful: