Created
January 29, 2020 12:41
-
-
Save laddered/5e30337a6da0c2c3ddf84e1b6d7a3c4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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