advent-of-code/hacker-rank/mini-max-sum/main.go

65 lines
1.1 KiB
Go
Raw Normal View History

2024-10-26 12:03:06 -06:00
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
func miniMaxSum(arr []int32) {
min := arr[0]
max := arr[0]
sum := int(arr[0])
for _, v := range arr[1:] {
if min > v {
min = v
}
if max < v {
max = v
}
sum += int(v)
}
fmt.Print(sum-int(max), sum-int(min))
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
arrTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var arr []int32
for i := 0; i < 5; i++ {
arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64)
checkError(err)
arrItem := int32(arrItemTemp)
arr = append(arr, arrItem)
}
miniMaxSum(arr)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}