Created
June 9, 2023 21:15
-
-
Save oscarito9410/d7ddaad430b7f907868e9c79a56fa4af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun missingNumber(input: IntArray): Int { | |
val n = input.size + 1 | |
val expectedSum = (n * (n + 1)) / 2 | |
val actualSum = input.sum() | |
return expectedSum - actualSum | |
} | |
fun missingNumber(input: IntArray): Int { | |
if(input.isEmpty() || input.count() == 1) { | |
return 1 | |
} | |
val expectedArray = IntArray(input.count() + 1) { it + 1 } | |
return expectedArray.subtract(input.asIterable()).toList().firstOrNull() ?: 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment