-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10 MissingInteger
More file actions
42 lines (31 loc) · 1.26 KB
/
10 MissingInteger
File metadata and controls
42 lines (31 loc) · 1.26 KB
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
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// O(N) or O(N*log(N))
/*-------------------------------------------------------------------------------
This is a demo task.
Write a function:
function solution(A);
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].
-------------------------------------------------------------------------------/*
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let positiveArray = new Array(A.length+1).fill(0);
for (let i=0; i<A.length; i+=1) {
if(A[i] > 0) {
let incrIndex = A[i]-1;
positiveArray[incrIndex] += 1;
}
}
for (let i=0; i< positiveArray.length; i+=1) {
if(positiveArray[i] == 0) {
return i+1;
}
}
return 1;
}