To rotate an array cyclically by one, you can perform the following steps:
- Store the last element of the array in a temporary variable.
- Traverse the array from the second-last element to the first element, and for each element, do the following: a. Assign the value of the previous element to the current element.
- Assign the value of the temporary variable to the first element of the array.
Original array: 1, 2, 3, 4, 5
| | | | |
v v v v v
1, 2, 3, 4, 5
^ |
| v
5, 1, 2, 3, 4
Here’s the Kotlin code to perform these steps:
fun main() {
val arr = intArrayOf(1, 2, 3, 4, 5)
println("Original array: ${arr.joinToString(", ")}")
rotateArrayByOne(arr)
println("Array after rotating by one: ${arr.joinToString(", ")}")
}
The rotateArrayByOne
function is designed to rotate the elements of an integer array by one position in a cyclic manner. Here’s a detailed explanation of how the function works:
- The function takes an input array
arr
of typeIntArray
as a parameter. - The function then initializes a temporary variable
temp
with the value of the last element of the input array. This is done by accessing the last element of the array using the expressionarr[arr.size - 1]
. - Next, the function iterates over the elements of the input array in reverse order, starting from the second-last element and going all the way up to the first element. This is done using a for loop with the expression
for (i in arr.size - 2 downTo 0)
. - For each element in the input array, the function assigns the value of the previous element to the current element. This is done using the expression
arr[i + 1] = arr[i]
. Note that we’re assigning the value of the previous element to the current element and not the other way around because we’re iterating over the array in reverse order. - After all the elements of the input array have been shifted to the right by one position, the function assigns the value of the temporary variable
temp
to the first element of the input array. This is done using the expressionarr[0] = temp
. - Finally, the function returns control back to the calling function, with the input array
arr
now containing the elements rotated by one position in a cyclic manner.
main
function first initializes an integer array arr
with the elements [1, 2, 3, 4, 5]
. It then prints out the original array using joinToString
to convert the array to a string with comma-separated values.
Next, it calls the rotateArrayByOne
function with the arr
array as an argument to rotate the array by one position.
Finally, it prints out the rotated array using joinToString
again to convert the rotated array to a string with comma-separated values.
When you run this main
function, you should see the following output:
Original array: 1, 2, 3, 4, 5
Array after rotating by one: 5, 1, 2, 3, 4
The time complexity of the rotateArrayByOne
function is O(n), where n is the length of the input array. This is because the function performs a single pass over the array, visiting each element once, to rotate the array by one position. The time taken to perform each operation in the function (e.g., accessing array elements, variable assignment, etc.) is constant and independent of the size of the input array, so the overall time complexity of the function is proportional to the length of the input array, i.e., O(n). Therefore, the function has a linear time complexity.