Created
May 19, 2023 15:00
-
-
Save iafsilva/0e8527ab82e6c1e707bb85e6250b6618 to your computer and use it in GitHub Desktop.
MissingPermElem
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 solution(a: IntArray): Int { | |
// First edge case: missing is the first nr | |
if (a.isEmpty()) return 1 | |
a.sort() | |
for (i in a.indices) { | |
// Index 0 must be 1, Index 1 must be 2, and so on. | |
// Whenever that's not true, we found our missing nr | |
if (a[i] != i + 1) return i + 1 | |
} | |
// Second edge case: missing is the last nr | |
return a.size + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment