Skip to content

Commit

Permalink
MissingInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
zengfenfei committed Apr 26, 2016
1 parent a2b76ce commit 209a465
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions MissingInteger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package codility

// https://codility.com/programmers/task/missing_integer/
func MissingInteger(A []int) int {
positiveMin := 0
for _, e := range A {
if e > 0 && (positiveMin == 0 || e < positiveMin) {
positiveMin = e
}
}
if positiveMin == 0 || positiveMin > 1 {
return 1
}

n := len(A)
possibleMissings := make([]bool, n-1)
for _, e := range A {
minOffset := e - positiveMin - 1
if minOffset >= 0 && minOffset < len(possibleMissings) {
possibleMissings[minOffset] = true
}
}
for i, e := range possibleMissings {
if !e {
return positiveMin + i + 1
}
}
return positiveMin + n
}

0 comments on commit 209a465

Please sign in to comment.