You are given an integer ‘N,’ and your task is to find the minimum steps to reach from 1 to 'N'. There are also ‘M’ mines on which you can not move.
In each step, you can move in the following way:
1 - You move either 1 or 2 steps forward.
2 - Whenever you are at a prime number, you can make a special move, where you can move to the sum of digits in (Count the number of primes before the current prime + Difference between the next prime and current number) until it becomes a single-digit number.
3- If you make a special move, you can’t make another special move for the next ‘L’ moves.
For Example:
You are given ‘N’ = 10, L = 2, and mines = [6], Here you can make the following moves for minimum path 1 -> 3 -> 5 (special move) -> 9 -> 10. Hence the answer is 4.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains three space-separated integers ‘N’, ‘L’ and ‘M’, representing the position to reach, the given integer, and the number of mines, respectively.
The next lines contain ‘M’ space-separated integers representing the position of each mine.
Output Format:
For each test case, print a single integer denoting the minimum number of steps required to reach ‘N.’
Print a separate line for each test case.
1 <= T <= 10
1 <= M < N <= 10^5
0 <= L <= 10
It is guaranteed that the sum of 'N' and the sum of 'M' over all test cases doesn't exceed 10^5.
Time Limit: 1 sec.
Note:
You do not need to print anything. It has already been taken care of. Just implement the function.
2
10 2 1
6
14 2 1
8
4
4
For the first test case, ‘N’ = 10, L = 2, and mines = [6], Here you can make the following moves for minimum path 1 -> 3 - >5(special move) -> 9->10. Hence the answer is 4.
For the second test case, 'N' = 11, L = 2 and mines = [8], Here you can make the following moves for 1 -> 3-> 6 -> 7(special move) -> 11.
2
12 1 1
10
6 1 1
4
5
2
Try to use recursion to find all the possible paths.
In this approach, we will visit all the possible places we can visit from the current position. If any of the position is not possible, we will return maximum int.
We can go to one or two places ahead unless it’s a prime number, then check for a special move from any number we are currently on.
We create two utility functions: getPrimes (n), which returns an array of prime numbers for 2 to n, and the other function, digitalSum(n), returns the sum of digits until it’s a single-digit number.
We will also create a utility function util(currentPos, curr), where currentPos is the current position of the number and curr is the normal moves left before a special move is allowed.
Algorithm:
O(2^N), Where N is the final position to be reached.
There are two or three different recursive calls for each step, which will cost on average O(2^N) time. Hence the final time complexity is O(2^N).
O(N), Where N is the final position to be reached.
The total space complexity of the recursive stack, primes array, and map is O(N) in the worst case. Hence the overall space complexity is O(N)