Find the Missing Number
MediumArrays
The problem
An array holds the numbers 1 to n with exactly one missing. Find it using the sum formula.
Sample input
[1, 2, 4, 5], n = 5
Sample output
3Show the solution · 8 languages
fun main() {
val arr = intArrayOf(1, 2, 4, 5)
val n = 5
val expected = n * (n + 1) / 2
println(expected - arr.sum())
}