Skip to content

Instantly share code, notes, and snippets.

@pablopalacios
Created November 23, 2023 09:46
Show Gist options
  • Save pablopalacios/4c43bbda77741052ac6b77bd0f45ab76 to your computer and use it in GitHub Desktop.
Save pablopalacios/4c43bbda77741052ac6b77bd0f45ab76 to your computer and use it in GitHub Desktop.
function solution(a) {
let jumps = 0;
let i = 0;
let currrentElement = 0
let nextElement = -1 ;
let lastElement = -1 ;
if(a[i] === 0) return -1
while (a[i]){
lastElement = currrentElement
currrentElement = i ;
jumps++;
i = i + a[i]
nextElement = i
if (lastElement === nextElement )
return -1
}
return jumps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment