Write a function to rotate an array k times to the right.
MediumProblem Statement
Write a function to rotate an array k times to the right.
Sample Input
A hardcoded array [1, 2, 3, 4, 5] and k = 2
Sample Output
Rotated array: 4, 5, 1, 2, 3
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
fun rotateArray(arr: Array<Int>, k: Int): Array<Int> {
// Add your code here to rotate the array
}
fun main() {
val array = arrayOf(1, 2, 3, 4, 5)
val k = 2
println("Rotated array: ${rotateArray(array, k).joinToString(", ")}")
}
Input
A hardcoded array [1, 2, 3, 4, 5] and k = 2
Expected Output
Rotated array: 4, 5, 1, 2, 3