Solution Review: Maximum, Minimum Array
This review provides a detailed analysis of the different ways to solve the maximum-minimum array problem.
First solution
First, we make a copy of the input array in an auxiliary array. Then we traverse the auxiliary array from the beginning to the end and alternately insert these values into the input array. For example, we insert the last element at arr[0]
, then insert the first element at arr[1]
, and so on.
Solution code
Press + to interact
package mainimport ("fmt")func MaxMinArr(arr []int, size int) {aux := make([]int, size)copy(aux, arr)start := 0stop := size - 1for i := 0; i < size; i++ {if i%2 == 0 {arr[i] = aux[stop]stop -= 1} else {arr[i] = aux[start]start += 1}}}/* Testing code */func main() {arr := []int{1, 2, 3, 4, 5, 6, 7}size := len(arr)MaxMinArr(arr, size)fmt.Println(arr)}
Complexity analysis
The time and space complexity of this program is ...