FinTech | Phone | Number of jumps
Anonymous User
260

Given an array of positive integers, return number of jumps made in given array. Jump - A number at given index can jump at max two position towards left. If any of number jump is more than two places then return -1.
Example:
Input : 1, 3, 2, 4, 5, 6, 7, 8
Output : 1 [explanation: 3 jumped once, 1]
Input : 3, 1, 2, 4, 5, 6, 8, 7
Output : 3 [explanation: 3 jumped twice, 8 jumped once, 2 + 1]
Input : 3, 1, 2, 8, 4, 5, 6, 7
Output : -1 [explanation: 3 jumped twice, 8 jumped 4 times]
Input : 2, 3, 1, 4, 5, 6, 8, 7
Ouput : 5 [explanation: 3 jumped twice, 2 jumped twice, 8 jumped 4 times, 2 + 2 + 1]

Any hints or suggestions on how to approach this problem?

Comments (2)