Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Created March 24, 2020 21:30
Show Gist options
  • Save victorkurauchi/e60ba7920deef3d8817fa07ff65b6de4 to your computer and use it in GitHub Desktop.
Save victorkurauchi/e60ba7920deef3d8817fa07ff65b6de4 to your computer and use it in GitHub Desktop.
Returns the smallest positive integer (greater than 0) that does not occur in A
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let sorted = A.filter(a => a >= 1).sort((a, b) => a - b);
// console.log('sorted', sorted)
let minor = 1;
for (let i = 0; i < sorted.length; i++) {
console.log('minor', minor, 'sorted:', sorted[i], minor < sorted[i]);
if (minor < sorted[i]) {
return minor;
}
minor = sorted[i] + 1;
}
console.log('final', minor)
return minor;
}
/*
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].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment