-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_04.go
More file actions
33 lines (29 loc) · 774 Bytes
/
problem_04.go
File metadata and controls
33 lines (29 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
const (
MaxUint = ^uint(0)
MaxInt = int(MaxUint >> 1)
)
/*
Task: Given an array of integers, find the first missing positive integer in linear time
and constant space. In other words, find the lowest positive integer that does not exist in the array.
The array can contain duplicates and negative numbers as well.
**/
func FirstMissingPositive(array []int) int {
min := MaxInt
missing := 0
for _, elem := range array {
if elem < min && elem >= 1 {
//in case that the delta is larger than 1 then there is a larger gap then just one
if (min-elem) > 1 && missing != elem {
missing = elem + 1
}
min = elem
} else if min+1 == elem {
missing = elem + 1
}
}
if min > 1 && min > missing {
return min - 1
}
return missing
}