/*
Computes the median of three integers.
The median is the value that is neither the minimum nor the maximum.
Formula:
median = a + b + c - min(a, b, c) - max(a, b, c)
*/
fun medianOfThreeNumbers(a: Int, b: Int, c: Int): Int {
// Determine the minimum of the three numbers
val minVal = when {
a <= b && a <= c -> a
b <= a && b <= c -> b
else -> c
}
// Determine the maximum of the three numbers
val maxVal = when {
a >= b && a >= c -> a
b >= a && b >= c -> b
else -> c
}
// The median is the remaining value
return a + b + c - minVal - maxVal
}
fun main() {
// Test cases
println("The median of [1, 1, 1] is ${medianOfThreeNumbers(1, 1, 1)}")
println("The median of [10, 3, 7] is ${medianOfThreeNumbers(10, 3, 7)}")
println("The median of [10, -10, -10] is ${medianOfThreeNumbers(10, -10, -10)}")
println("The median of [3, 3, 5] is ${medianOfThreeNumbers(3, 3, 5)}")
}
/*
run:
The median of [1, 1, 1] is 1
The median of [10, 3, 7] is 7
The median of [10, -10, -10] is -10
The median of [3, 3, 5] is 3
*/