Kmspico Download | Official KMS Activator Website [New Version 2024] Fast and Easy Converter YouTube to MP3 Online KMSAuto Net Activator Download 2024 Immediate Byte Pro Neoprofit AI Blacksprut without borders. Discover new shopping opportunities here where each link is an entrance to a world ruled by anonymity and freedom.

Find the Minimum and Maximum Element in an Array Using Kotlin

You can find the minimum and maximum elements in an array in Kotlin using the following code:

fun findMinAndMax(arr: IntArray): Pair<Int, Int> {
if (arr.isEmpty()) {
throw IllegalArgumentException("Array is empty")
}
var min = arr[0]
var max = arr[0]
for (element in arr) {
if (element < min) {
min = element
}
if (element > max) {
max = element
}
}
return Pair(min, max)
}
fun main() {
val array = intArrayOf(4, 2, 8, 1, 6, 9, 3, 7)
try {
val result = findMinAndMax(array)
println("Minimum: ${result.first}")
println("Maximum: ${result.second}")
} catch (e: IllegalArgumentException) {
println(e.message)
}
}

This code defines a function `findMinAndMax` that takes an `IntArray` as input and returns a `Pair<Int, Int>` containing the minimum and maximum elements. The `main` function demonstrates its usage with a sample array.