Skip to content

Instantly share code, notes, and snippets.

@laddered
Created January 29, 2020 12:41
Show Gist options
  • Save laddered/5e30337a6da0c2c3ddf84e1b6d7a3c4a to your computer and use it in GitHub Desktop.
Save laddered/5e30337a6da0c2c3ddf84e1b6d7a3c4a to your computer and use it in GitHub Desktop.
function solution(X, A) {
if (A.length === 1) {
if (A[0] === 1 && X === 1) return 0;
else return -1;
}
i = -1
sumOfArr = 0
sequenceSum = (X * (X+1)) / 2
arrOfFound = []
do {
i++;
if (arrOfFound[A[i]]) continue;
arrOfFound[A[i]] = true;
sumOfArr += A[i];
if (sumOfArr === sequenceSum) break;
} while (i < A.length)
if (i === A.length) return -1;
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment