Skip to content

Instantly share code, notes, and snippets.

@matheus1lva
Created October 22, 2024 20:26
Show Gist options
  • Save matheus1lva/9f6cd1eee967ec0f226531676b916fbb to your computer and use it in GitHub Desktop.
Save matheus1lva/9f6cd1eee967ec0f226531676b916fbb to your computer and use it in GitHub Desktop.
/**
* “Using Javascript or Typescript, please write runnable code to solve this challenge.
You are given an array of integers of arbitrary size. The numbers in the array are in sorted order.
The numbers are incremented sequentially by one, somewhere in the sequence there is a missing number.
Write code, complete functions, that can parse such an arbitrary size array and return the missing number.
number 15 is missing here
An example array could be: [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20]
*/
function findMissing(sortedNumbers) {
let missingNumber = null;
for(let i = 0; i < sortedNumbers.length; i++) {
if(i > 1) {
const previous = sortedNumbers[i-1];
const current = sortedNumbers[i];
if(current-1 !== previous) {
missingNumber = current-1;
break;
}
}
}
return missingNumber;
}
/**
* O(n)
* omega(n-[i])
*/
const input =[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20];
console.log(findMissing(input));
function findMissingFaster(sortedArray) {
console.log(sortedArray.length);
const middleSize = Math.floor(sortedArray.length/2);
const firstHalf = sortedArray.slice(0,middleSize);
const lastHalf = sortedArray.slice(middleSize);
}
console.log('findMissingFaster', findMissingFaster(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment