Write a function:
function solution(A);
that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A. For example, given:
A[0] = 1 A[1] = 3 A[2] = 6 A[3] = 4 A[4] = 1 A[5] = 2
the function should return 5. Assume that:
• N is an integer within the range [1..100,000]; • each element of array A is an integer within the range
[−2,147,483,648..2,147,483,647].
Complexity: • expected worst-case time complexity is O(N); • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
My Answer is 100% WRONG! What is wrong with it? First let me state the obvious errors
Assumptions I made that may be wrong
my code, which works on own test cases, and works on negative numbers too, got 0%.
function solution(A) {
// write your code in JavaScript (Node.js 0.12)
A.sort();
var a_length = A.length;
for(var i = 0; i < a_length; i++){
// if i is 0 - 1 = -1 then do not do the following
// if i is 1 - 1 - 0 then do the follow
// if i >= 0 then do the following
if(i - 1 >= 0){
// remember above there is a A.sort() so it
// looks like this
// A[0] = 1
// A[1] = 1
// A[2] = 2
// A[3] = 3
// A[4] = 4
// A[5] = 6
// if A[1] is 1 and A[1-1 = 0] is 1 then this is 1>1 false
// if A[2] is 2 and A[2-1 = 1] is 1 then this is 1>1 false
// if A[3] is 3 and A[3-1 = 2] is 2 then this is 1>1 false
// if A[4] is 4 and A[4-1 = 3] is 3 then this is 1>1 false
// if A[5] is 6 and A[5-1 = 4] is 4 then this is 2>1 true return A[i - 1] + 1 where A[5 - 1 = 4] is 4 + 1 is 5. 5 is returned.
if(A[i] - A[i - 1] > 1){
return A[i - 1] + 1;
}
}
}
// this does not check for negative
// returns the minimal positive integer (greater than 0
// this is a return no minimal positive integer found
return 0;
}
Everything is wrong, example test result:
simple simple test 0.072 s WRONG ANSWER got 3 expected 1
Why does it work for me and not for them.
function solution(A) {
var min = 1;
A.sort(function(a,b){
// Sort the array explicit way
return a - b;
});
for (var i in A) {
if (A[i] > -1 && A[i] == min) {
min++;
}
}
return min;
}
There is my solution with JavaScript for Codility MissingInteger (got 100/100)
function solution(A) {
const len = A.length;
const hash = {};
for (let i = 0; i < len; i++) {
// here we are making an object with all
// numbers in a given array as object keys and values
// if 0 is given as possible digit we could assing
// hash[A[i]] = true; or any truthy value
hash[A[i]] = A[i];
}
for (let i = 1; i < 1000002; i++) {
// here we are trying to find any number
// between 1 and 1000001 (given constraints)
// which do not exists in an object
// if the number is not in the object that's our missing one
if(!hash[i]) return i;
}
return 1;
}
For this problem I like to start by sorting the given array. I then iterate through the sorted array with a reduce. I give the reduce an accumulator acc
initially equal to 1
(that's what the 1 after the comma is for). Only when the element val
is equal to the accumulator do I increment the accumulator. Otherwise, I return the accumulator as is. When I can no longer find an element in the array equal to the accumulator, that accumulator is the lowest missing positive integer.
const solution = A => {
A.sort((a, b) => a - b);
return A.reduce((acc, val) => acc === val ? acc + 1 : acc, 1);
}
I know this question's been around for a while but I hope this answer is useful for someone. I use Array.prototype.sort(), Array.prototype.reduce(), and a ternary in this answer. A knowledge of those patterns should give more insight into this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With