Given a sequence of numbers, find the minimum number of jumps to go from the starting position to the end and come back to the starting position again.
Each element of the sequence denotes the maximum number of moves that one can move from that position.
At any position, you can make a jump of at max k moves, where k is the value stored at that position. After reaching the end, you can use only those positions for jumping which have not been used previously for jumping.
The input will be given as a sequence of numbers separated by single-spaces.
Output should be a single number which is the minimum number of jumps used.
If its not possible to go to the end and come back to the starting position, then print -1
Input:
2 4 2 2 3 4 2 2
Output:
6 (3 to reach end and 3 to come back)
Input
1 0
Output
-1
Note
- Assume all the numbers of the sequence are non-negative
EDIT 1
The line "Thus, it should be clear that one can always jump from the last position." might be confusing, so I removed it from the question. It will have no effect on the question.
Winning Criteria :
The winner will be the one with the shortest code.
Data.List.permutationsdoes not work in GHC, but only in GHCi. According to our Guide to Golfing Rules in Haskell, you should either add the import or mark your answer as "Haskell GHCi". The first option is generally preferred by Haskell golfers on this site. \$\endgroup\$a<-permutations[0..t l-1],let f=takeWhile(/=0)a, you can writef<-map(takeWhile(/=0))(permutations[0..t l-1]), which again can be golfed tof<-fst.span(>0)<$>permutations[0..t l-1]. With this you are back to 166 bytes even by adding the import: Try it online! \$\endgroup\$