Skip to content

Instantly share code, notes, and snippets.

@oscarito9410
Created June 9, 2023 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarito9410/d7ddaad430b7f907868e9c79a56fa4af to your computer and use it in GitHub Desktop.
Save oscarito9410/d7ddaad430b7f907868e9c79a56fa4af to your computer and use it in GitHub Desktop.
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