fork download
  1. fun findMinMax(array: List<Int>): Pair<Int,Int> {
  2. var minCandidates = mutableListOf<Int>()
  3. var maxCandidates = mutableListOf<Int>()
  4.  
  5. for (i in 1 until array.count() step 2) {
  6. if (array[i - 1] < array[i]) {
  7. minCandidates.add(array[i - 1])
  8. maxCandidates.add(array[i])
  9. } else {
  10. minCandidates.add(array[i])
  11. maxCandidates.add(array[i - 1])
  12. }
  13. }
  14.  
  15. if (array.count() % 2 != 0) {
  16. minCandidates.add(array[array.count() - 1])
  17. maxCandidates.add(array[array.count() - 1])
  18. }
  19.  
  20. var min = minCandidates[0]
  21. var max = maxCandidates[0]
  22.  
  23. for (i in minCandidates.indices) {
  24. if (min > minCandidates[i]) {
  25. min = minCandidates[i]
  26. }
  27.  
  28. if (max < maxCandidates[i]) {
  29. max = maxCandidates[i]
  30. }
  31. }
  32.  
  33. return Pair(min, max)
  34. }
  35.  
  36. fun main() {
  37. val array = listOf(3, 6, 1, 9, 10, 4, -4, 6, 12, 5, 11)
  38.  
  39. val (min, max) = findMinMax(array)
  40.  
  41. println("Minimum: $min, Maximum: $max")
  42. }
Success #stdin #stdout 0.1s 42568KB
stdin
Standard input is empty
stdout
Minimum: -4, Maximum: 12