Array Rotate
~3 mins read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
left rotate a size n array by d
5 4
1 2 3 4 5
5 1 2 3 4
*/
package main
import "fmt"
func reverse(nums []int) {
for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
}
func leftRotateBySwapping(arr []int, d int) []int {
n := len(arr)
d %= n // Handle cases where d is greater than n
// Reverse the first part of the array (0 to d-1)
reverse(arr[:d])
// Reverse the second part of the array (d to n-1)
reverse(arr[d:])
// Reverse the entire array to obtain the final rotated array
reverse(arr)
return arr
}
func leftRotateDirect(arr []int, d int) []int {
n := len(arr)
result := make([]int, n)
for i := 0; i < n; i++ {
// Calculate the new index after left rotation
and store the element in the result slice
result[(i+n-d)%n] = arr[i]
}
return result
}
func main() {
// Example usage of the leftRotateBySwapping function
array := []int{1, 2, 3, 4, 5}
d := 2
rotatedArray := leftRotateBySwapping(array, d)
fmt.Println(rotatedArray) // Print the rotated array
}