Split the Number String into Primes in Java10 Sept 2024 | 11 min read A string is given that is made only of digits, such that the string represents a number. Our task is to split the number string in such a way that each segment of the number that is formed after the split is a prime number. Also, we have to take care to minimize the number of splits. Note that if the input string itself represents a prime number, then the minimum split is 1 as we are taking it as a whole. The prime numbers found have to be in the range of 1 to 10 ^ 6. Let's understand it with the help of a few examples. Example 1: Input: "13477317" Output: 2 Explanation: If we take 1 split, then we have to take 13477317 as a whole, and the number 13477317 is divisible by 3. Hence, 1 split is not possible. Now, we take 2 splits, and we see that 13477 is a prime number and 317 is also a prime number. Thus, in 2 splits, we got all the segments as prime numbers. Example 2: Input: "17" Output: 1 Explanation: If we take 1 split, then we have to take 17 as a whole, and the number 13477317 is a prime number. Hence, 1 split will suffice. Let's see different approaches to find the splits. Using RecursionThe concept is to take into consideration each prefix up to 6 digits (as it is already known that the prime numbers are < 10 ^ 6) and validate whether it is a prime number or not. If it is a prime number, then recursively invoke the method to validate the string that is remaining. If any of the combinations do not give the appropriate arrangement, then an appropriate message is shown on the console. FileName: SplitStringPrimes.java Output: For the number 343, the minimum number of splits is: 2 For the number 37, the minimum number of splits is: 1 For the number 44, the minimum number of splits is not possible. For the number 367555, the minimum number of splits is: 4 Complexity Analysis The time complexity of the above program is O(N5 / 2), where O(N2) time is taken to recursively find all the possible combinations. As we also have to check whether the number is prime or not, and this task takes the time complexity of O(N1 / 2). Thus, the total time complexity of the above program is O(N2) * O(N1 / 2) = O(N5 / 2), where N is the total number of digits present in the input string. Using IterationUsing iteration, one can solve the problem with the help of dynamic programming. We can define an array splitArr[] and use it where splitArr[q] represents the minimum number of splits that are required in the prefix string of the length 'q' to split it into the subdivision of prime. The array splitArr[] is getting filled in the following manner:
When all of the values are updated, the value present at the last index is our answer as it contains the least splits number for the complete input string. FileName: SplitStringPrimes1.java Output: For the number 343, the minimum number of splits is: 2 For the number 37, the minimum number of splits is: 1 For the number 44, the minimum number of splits is not possible. For the number 367555, the minimum number of splits is: 4 Complexity Analysis The time complexity of the above program is O(N3 / 2), where O(N) time is taken to traverse through all the indices. We also have to check whether the number is prime or not, and this task takes the time complexity of O(N1 / 2). Thus, the total time complexity of the above program is O(N) * O(N1 / 2) = O(N3 / 2), where N is the total number of digits present in the input string. We know that the prime numbers range from 1 to 10 ^ 6 in this problem. We can use this information for further optimization. Further optimization is possible when we precompute the checks of the prime number and store it in an array. The precomputation is possible with the help of the sieve of the Eratosthenes technique. Observe the following program. FileName: SplitStringPrimes2.java Output: For the number 343, the minimum number of split is: 2 For the number 37, the minimum number of split is: 1 For the number 44, the minimum number of split is not possible. For the number 367555, the minimum number of split is: 4 Complexity Analysis It is the most optimized approach as the time complexity of the above program is O(N), where N is the total number of digits present in the input string. Though the time complexity of computing the sieve is O(N * log(N)), we can ignore it as it is computed only once. Thus, unlike other programs, we can check whether a number is a prime number or not in just O(1) time. Next TopicJava Cron Expression |
Our primary focus is on the set of vowels because the set of vowels is usually essential to many string manipulation problems, and one such problem is the problem of identifying the longest substring of the given string containing precisely K different vowels. The problem...
6 min read
A XOR tree is a binary tree where each node's value is the XOR of all values in its subtree, including itself. Converting a given binary tree into a XOR tree involves a post-order traversal, calculating the XOR for each node from its children up to...
10 min read
The problem is to transform an integer and represent it as a sequence of binary digits and then to determine the most significant sequence of zeros that one's enclose. In other words, if the binary representation string does not contain any zeros placed between ones,...
6 min read
? In order to write more adaptable, reusable, and type-safe code, developers need to use the Java programming language's generics capability. Generics were initially made available in Java 5 and have since established themselves as a key component in any Java developer's toolbox. In this section, we...
4 min read
Greater Node in Linked List in Java Given a linked list L of integers, the task is to return an integer linked list that includes the bigger element for every element in the supplied linked list. In the case that no element has a greater...
6 min read
The scope of a variable determines where in the program a variable can be accessed and modified. Java follows strict variable scoping rules to ensure variables are used correctly and do not interfere with other variables. Scope of a variable can be determined at compile...
6 min read
In the world of concurrent programming, managing shared data and ensuring thread safety are crucial aspects. Java, being a popular programming language, provides robust features to handle concurrency. One such concept is the Concurrent Array, which allows multiple threads to access and modify elements concurrently without...
4 min read
In the field of computer programming, the maximum product subarray problem is a popular challenge that requires finding the contiguous subarray within an array of integers that has the largest product. This problem can be efficiently solved using dynamic programming techniques. In this article, we will...
4 min read
In the world of Java programming, interfaces play a vital role in defining contracts and establishing communication between classes. Typically, an interface is used to declare a set of methods that implementing classes must adhere to. However, Java also allows the creation of interfaces without any...
4 min read
The term Lexicographical order is a mathematical term known by names: lexical order, lexicographic(al) product, alphabetical order, or dictionary order. This section will cover the topic lexicographical order, its definition, and other detailed information. After that, we will learn how to use the concept of lexicographical order...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India