# Golang Program to cyclically permutes the elements of the array

Cyclically means that in every cycle we wish to move the array elements by one position till we get the original array again. Cyclic permutation can be useful in various matrix manipulation and linear algebra operations. In this article, we are going to see different examples to permute the elements of the array cyclically using go programming language.

## Syntax

func append(slice, element_1, element_2…, element_N) []T


The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of an array containing all the values.

func make ([] type, size, capacity)


The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments and returns the slice that we can store in the variable.

## Algorithm

• Step 1 − First, we need to import the fmt package.

• Step 2 − Then start the main() function. Here in this function initialize an array of integers using make() function and append values to it using append() function.

• Step 3 − Declare a new variable called n and store the length of array to it. Further, print the array on the screen.

• Step 4 − Use a for loop to iterate over the array and shift the elements of the array to left by one position and print the new array thus formed.

• Step 5 − Print the new array in each Step until whole of the array is shifted cyclically.

## Example 1

In this example, we will use a for loop to permute the elements of the array in cyclic manner. We will use the for loop to iterate over the array and shift the element of the array to left by one position in each Step.

package main
import "fmt"
func main() {
// initializing an array
arr := make([]int, 0, 5)
arr = append(arr, 1, 2, 3, 4, 5)
n := len(arr)
fmt.Println("The given array is: ", arr)
for i := 0; i < n; i++ {
last := arr[n-1]
for j := n - 1; j > 0; j-- {
arr[j] = arr[j-1]
}
arr = last
fmt.Println("Permuted Array: ", arr)
}
}


## Output

The given array is:  [1 2 3 4 5]
Permuted Array:  [5 1 2 3 4]
Permuted Array:  [4 5 1 2 3]
Permuted Array:  [3 4 5 1 2]
Permuted Array:  [2 3 4 5 1]
Permuted Array:  [1 2 3 4 5]


## Example 2

In this example, we will use the internal append() function of the go programming language to cyclically permute the elements of the array till we get the original array again.

package main
import "fmt"
func main() {
// initializing an array
arr := make([]int, 0, 5)
arr = append(arr, 1, 2, 3, 4, 5)
n := len(arr)
fmt.Println("Original Array: ", arr)
for i := 0; i < n; i++ {
arr = append(arr[1:], arr)
fmt.Println("Permuted Array: ", arr)
}
}


## Output

Original Array:  [1 2 3 4 5]
Permuted Array:  [2 3 4 5 1]
Permuted Array:  [3 4 5 1 2]
Permuted Array:  [4 5 1 2 3]
Permuted Array:  [5 1 2 3 4]
Permuted Array:  [1 2 3 4 5]


## Conclusion

We have successfully compiled and executed a go language program to cyclically permute the elements of the array. Here we have used two programs. In the first program we are using a for loop to shift the elements of the array by one position till we get the original array again while in the second program we are using an internal function append() to perform the logic and print the array after each Step.