Created
October 1, 2017 05:28
-
-
Save johndavedecano/ddeedfa367fb739b6b5f8bff55f2cdf2 to your computer and use it in GitHub Desktop.
EXAMS - SHORTEST SEQUENCE
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(A) { | |
let arr = [1]; | |
while (arr[arr.length - 1] < A) { | |
let prev = arr[arr.length - 1]; | |
let next = prev * 2; | |
if (next > A) { | |
prev = arr[arr.length - 2] + 1; | |
next = prev * 2; | |
} | |
arr[arr.length - 1] = prev; | |
arr[arr.length] = next; | |
} | |
console.log(arr); | |
return arr.length; | |
} | |
console.log(solution(18)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment