Merge pull request #1161 from leonardogonfiantini/add-go-reverse-array

chore(Go): Add reverse array
main
Ayomide AJAYI 2023-06-09 17:14:29 +02:00 committed by GitHub
commit d3c2184af8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,19 @@
//Description : This program reverses array elements by swapping the first half part of the array
//Time Complexity : O(n/2), where n is the array size
//Auxiliary Space : O(1)
package arrays
import ("fmt")
func ReverseArray(arr []int) {
// get the length of the array
n := len(arr)
// iterate over half of the array and swap corresponding elements
for i := 0; i < n/2; i++ {
arr[i], arr[n-i-1] = arr[n-i-1], arr[i]
}
// print the reversed array
fmt.Println(arr)
}