![]() |
VOOZH | about |
Given a Binary string, the task is to find the largest Prime Number possible by the decimal representation of a subsequence of the given binary string. If no prime number can be obtained, print -1.
Examples:
Input: S = "1001"
Output: 5
Explanation: Out of all subsequences of the string "1001", the largest prime number that can be obtained is "101" (= 5).Input: "1011"
Output: 11
Explanation: Out of all subsequences of the string "1011", the largest prime number that can be obtained is "1011" (= 11).
Approach: To solve the problem, the idea is to generate all possible subsequences of the string, and convert each subsequence to its equivalent decimal form. Print the largest prime number obtained from this subsequences.
Follow the steps below to solve this problem:
Below is the implementation of the above approach:
3
Time Complexity: O(2N * ?N), where N is the length of the string.
Auxiliary Space: O(2N * N)