Given a number in the form of a string s, the task is to calculate and display minimum splits required such that the segments formed are Prime or print Not Possible otherwise.
Input: s = “2351”
Output : 0
Explanation: Given number is already prime.
Input: s = “2352”
Explanation: Resultant prime segments are 23,5,2
Input: s = “2375672”
Output : 2
Explanation: Resultant prime segments are 2,37567,2
Try all possible splits recursively and at each split, check whether the segments formed are prime or not. Consider a 2D array dp where dp[i][j] shows the minimum splits from index i to j and return dp[n] where n is the length of the string.
dp[i][j] = min(1 + solve(i, k) + solve(k + 1, j)) where i <= k <= j
Actually, in the exact recurrence written above, the left and right segments both are non-prime, then 1 + INT_MAX + INT_MAX will be negative which leads to incorrect answer.
So, separate calculations for the left and right segments are required. If any segment is found to be non-prime, no need to proceed further. Return min(1+left+right) otherwise.
Base cases considered are :
- If the number is prime, return 0
- If i==j and the number is prime, return 0
- If i==j and the number is not prime, return INT_MAX
Below code is the implementation of above approach:
- Count possible splits of sum N into K integers such that the minimum is at least P
- Count of ways to split a given number into prime segments
- Minimum number of cuts required to make circle segments equal sized
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Maximum number of splits of a binary number
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum number of given operations required to convert a permutation into an identity permutation
- Maximize the numbers of splits in an Array having sum divisible by 3
- Minimize splits to generate monotonous Substrings from given String
- Check if a large number can be divided into two or more segments of equal sum
- Split the array into odd number of segments of odd lengths
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Divide N segments into two non-empty groups such that given condition is satisfied
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Find element using minimum segments in Seven Segment Display
- Count of minimum reductions required to get the required sum K
- Minimum number of given operations required to convert a string to another string
- Minimum number of given operation required to convert n to m
- Minimum number operations required to convert n to m | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.