Skip to content

Instantly share code, notes, and snippets.

@iafsilva
Created May 19, 2023 15:00
Show Gist options
  • Save iafsilva/0e8527ab82e6c1e707bb85e6250b6618 to your computer and use it in GitHub Desktop.
Save iafsilva/0e8527ab82e6c1e707bb85e6250b6618 to your computer and use it in GitHub Desktop.
MissingPermElem
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