-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleftrotation.go
45 lines (37 loc) · 883 Bytes
/
leftrotation.go
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
package main
import "fmt"
func main() {
arr := []int{1,2,3,4,5}
arrB := make([]int, len(arr))
copy(arrB, arr)
n := 3
// Method 1: Rotate by one n times
for i := 0; i < n; i++ {
RotateLeftByOne(arr)
}
fmt.Println(arr)
// Method 2: Use temp array to rotate
RotateLeft(arrB, n)
fmt.Println(arrB)
}
func RotateLeftByOne(arr []int) {
temp := arr[0]
for i := 0; i < len(arr) - 1; i++ {
arr[i] = arr[i+1]
}
arr[len(arr)-1] = temp
}
func RotateLeft(arr []int, d int) {
d = d % len(arr)
// Store the first d items in a temp array
temp := make([]int, d)
copy(temp,arr[:d])
// Shift original slice
for i := 0; i < len(arr) - d; i++ {
arr[i] = arr[i+d]
}
// Store back the temp elements
for i := 0; i < len(temp); i++ {
arr[len(arr) - d + i] = temp[i]
}
}