


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.
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.
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.
You do not need to print anything. It has already been taken care of. Just implement the function.
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:
In this approach, we will use the same approach as before but we will create a memo 2D matrix of size NXL that will store all the answers for all pairs so N and L therefore we won’t have to recalculate them.
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.
Algorithm:
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum