Last active
August 25, 2017 06:33
-
-
Save eugeneglova/69aba8adfeef522f75e6c7f4c5f2b69c to your computer and use it in GitHub Desktop.
find missing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = [1, 3, 5, 2, 6, 7, 9, 4]; | |
list.sort(); | |
function find(list, acc = 0) { | |
if (list.length < 3 && list[1] - list[0] !== 1) return list[0] + 1; | |
const n = Math.round(list.length / 2); | |
const n1 = acc + n; | |
const realN = list[n - 1]; | |
return find(n1 * (n1 + 1) === realN * (realN + 1) ? list.slice(n - (list.length % 2 === 0 ? 0 : 1)) : list.slice(0, n), n1); | |
} | |
find(list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment