chef and digit jump

I wrote an o(n) code for the chef and digit question.but TLE is coming.So does the question demand even better algorithm or i have done some mistake in coding?

recheck your complexity analysis. O(n) is sufficient.

1 Like

thanks . I will try to rectify my mistakes.

What is meant by Chef can’t jump out from sequence, can he jump back to same same number

What is result for this test case

0 1 2 3 4 5 9 6 7 8 9 0 1 2 3 4 5

is it 5:
0 - 11 -> 10 -> 6 -> 5 -> 16

or 6

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 16

1 Like

ur input is 0123456789012345…then asnwer should be 6 …there are many pathss which lead to 6

@sand1n3 the answer will be 6 in both of the cases you mentioned.

why? The answer is 5 in the first case.

3 Likes

Its only 5 jumps from the start right

In second case one extra jump is there right

for 1st case: But u have to visit last element too. So, path will be 0-1-2-3-4-5-5

look at this

0 - 11 -> 10 -> 6 -> 5 -> 16 (these are indices), this have only 5 jumps and it also reached last element

how are you saying @vikascingh that the answer is 6 … it is only 5 … sandIn3 was right