fun findMinMax(array: List<Int>): Pair<Int,Int> {
var minCandidates = mutableListOf<Int>()
var maxCandidates = mutableListOf<Int>()
for (i in 1 until array.count() step 2) {
if (array[i - 1] < array[i]) {
minCandidates.add(array[i - 1])
maxCandidates.add(array[i])
} else {
minCandidates.add(array[i])
maxCandidates.add(array[i - 1])
}
}
if (array.count() % 2 != 0) {
minCandidates.add(array[array.count() - 1])
maxCandidates.add(array[array.count() - 1])
}
var min = minCandidates[0]
var max = maxCandidates[0]
for (i in minCandidates.indices) {
if (min > minCandidates[i]) {
min = minCandidates[i]
}
if (max < maxCandidates[i]) {
max = maxCandidates[i]
}
}
return Pair(min, max)
}
fun main() {
val array = listOf(3, 6, 1, 9, 10, 4, -4, 6, 12, 5, 11)
val (min, max) = findMinMax(array)
println("Minimum: $min, Maximum: $max")
}
ZnVuIGZpbmRNaW5NYXgoYXJyYXk6IExpc3Q8SW50Pik6IFBhaXI8SW50LEludD4gewogIHZhciBtaW5DYW5kaWRhdGVzID0gbXV0YWJsZUxpc3RPZjxJbnQ+KCkKICB2YXIgbWF4Q2FuZGlkYXRlcyA9IG11dGFibGVMaXN0T2Y8SW50PigpCgogIGZvciAoaSBpbiAxIHVudGlsIGFycmF5LmNvdW50KCkgc3RlcCAyKSB7CiAgICBpZiAoYXJyYXlbaSAtIDFdIDwgYXJyYXlbaV0pIHsKICAgICAgbWluQ2FuZGlkYXRlcy5hZGQoYXJyYXlbaSAtIDFdKQogICAgICBtYXhDYW5kaWRhdGVzLmFkZChhcnJheVtpXSkKICAgIH0gZWxzZSB7CiAgICAgIG1pbkNhbmRpZGF0ZXMuYWRkKGFycmF5W2ldKQogICAgICBtYXhDYW5kaWRhdGVzLmFkZChhcnJheVtpIC0gMV0pCiAgICB9CiAgfQoKICBpZiAoYXJyYXkuY291bnQoKSAlIDIgIT0gMCkgewogICAgbWluQ2FuZGlkYXRlcy5hZGQoYXJyYXlbYXJyYXkuY291bnQoKSAtIDFdKQogICAgbWF4Q2FuZGlkYXRlc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