Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active February 23, 2017 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerep/3cec17505541f7849c165dc0435ec86a to your computer and use it in GitHub Desktop.
Save gerep/3cec17505541f7849c165dc0435ec86a to your computer and use it in GitHub Desktop.
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func main() {
// Read from Stdin
b := bufio.NewReader(os.Stdin)
l, _, err := b.ReadLine()
if err != nil {
panic(err)
}
// Split converted buffer
n := strings.Split(string(l), " ")
// Convert number from string to int into the array
intList := []int{0, 0, 0, 0, 0}
for x := 0; x < 5; x++ {
i, err := strconv.Atoi(n[x])
if err != nil {
panic(err)
}
intList[x] = i
}
sort.Ints(intList)
sum := 0
for x := 0; x < 5; x++ {
sum += intList[x]
}
lower := sum - intList[4]
higher := sum - intList[0]
fmt.Printf("%d %d", lower, higher)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment