-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_coding_problem_4.test.js
60 lines (46 loc) · 1.31 KB
/
daily_coding_problem_4.test.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
This problem was asked by Stripe.
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.
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should
give 3.
You can modify the input array in-place.
*/
const missingPositiveInteger = (arr) => {
const size = arr.length
let i = 0
while (i < size) {
if (arr[i] > 0 && arr[i] <= size && arr[i] !== arr[arr[i] - 1]) {
const temp = arr[arr[i] - 1]
arr[arr[i] - 1] = arr[i]
arr[i] = temp
} else {
i++
}
}
for (i = 0; i < arr.length; i++) {
if (arr[i] !== i + 1) return i + 1
}
return size + 1
}
// TESTS ///////////////////////////////////////////////////////////////////////
test('#1', () => {
expect(missingPositiveInteger([3, 4, -1, 1])).toBe(2)
})
test('#2', () => {
expect(missingPositiveInteger([1, 2, 0])).toBe(3)
})
test('#3', () => {
expect(missingPositiveInteger([1, 2, 0, 1, -7])).toBe(3)
})
test('#4', () => {
expect(missingPositiveInteger([5, 3, 9])).toBe(1)
})
test('#5', () => {
expect(missingPositiveInteger([])).toBe(1)
})
test('#6', () => {
expect(missingPositiveInteger([-4])).toBe(1)
})