Given an integer K and a numeric string str, the task is to find the longest subsequence from the given string which is divisible by K.
Examples:
Input: str = “121400”, K = 8
Output: 121400
Explanation:
Since the whole string is divisible by 8, the entire string is the answer.Input: str: “7675437”, K = 64
Output: 64
Explanation:
The longest subsequence from the string which is divisible by 64, is “64” itself.
Approach: The idea is to find all subsequences of the given string and for each subsequence, check if its integer representation is divisible by K or not. Follow the steps below to solve the problem:
- Traverse the string. For every character encountered, two possibilities exists.
- Either consider the current character in the subsequence or not. For both the cases, proceed to the next characters of the string and find the longest subsequence that is divisible by K.
- Compare the longest subsequences obtained above with the current maximum length of longest subsequence and update accordingly.
- Repeat the above steps for every character of the string and finally, print the maximum length obtained.
Below is the implementation of the above approach.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to if the integer representation // of the current string is divisible by K bool isdivisible(string& newstr, long long K) { // Stores the integer // representation of the string long long num = 0; for (int i = 0; i < newstr.size(); i++) { num = num * 10 + newstr[i] - '0'; } // Check if the num is // divisible by K if (num % K == 0) return true; else return false; } // Function to find the longest subsequence // which is divisible by K void findLargestNo(string& str, string& newstr, string& ans, int index, long long K) { if (index == (int)str.length()) { // If the number is divisible by K if (isdivisible(newstr, K)) { // If current number is the // maximum obtained so far if ((ans < newstr && ans.length() == newstr.length()) || newstr.length() > ans.length()) { ans = newstr; } } return; } string x = newstr + str[index]; // Include the digit at current index findLargestNo(str, x, ans, index + 1, K); // Exclude the digit at current index findLargestNo(str, newstr, ans, index + 1, K); } // Driver Code int main() { string str = "121400"; string ans = "", newstr = ""; long long K = 8; findLargestNo(str, newstr, ans, 0, K); // Printing the largest number // which is divisible by K if (ans != "") cout << ans << endl; // If no number is found // to be divisible by K else cout << -1 << endl; } |
Java
// Java program for the // above approachimport java.util.*;import java.lang.*;class GFG{ // Function to if the integer representation // of the current string is divisible by K static boolean isdivisible(StringBuilder newstr, long K) { // Stores the integer // representation of the string long num = 0; for(int i = 0; i < newstr.length(); i++) { num = num * 10 + newstr.charAt(i) - '0'; } // Check if the num is // divisible by K if (num % K == 0) return true; else return false; } // Function to find the longest// subsequence which is divisible// by K static void findLargestNo(String str, StringBuilder newstr, StringBuilder ans, int index, long K) { if (index == str.length()) { // If the number is divisible by K if (isdivisible(newstr, K)) { // If current number is the // maximum obtained so far if ((newstr.toString().compareTo(ans.toString()) > 0 && ans.length() == newstr.length()) || newstr.length() > ans.length()) { ans.setLength(0); ans.append(newstr); } } return; } StringBuilder x = new StringBuilder( newstr.toString() + str.charAt(index)); // Include the digit at current index findLargestNo(str, x, ans, index + 1, K); // Exclude the digit at current index findLargestNo(str, newstr, ans, index + 1, K); } // Driver codepublic static void main (String[] args){ String str = "121400"; StringBuilder ans = new StringBuilder(), newstr = new StringBuilder(); long K = 8; findLargestNo(str, newstr, ans, 0, K); // Printing the largest number // which is divisible by K if (ans.toString() != "") System.out.println(ans); // If no number is found // to be divisible by K else System.out.println(-1); }}// This code is contributed by offbeat |
121400
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
- Remove characters from a numeric string such that string becomes divisible by 8
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest subsequence whose sum is divisible by a given number
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Find length of longest subsequence of one string which is substring of another string
- Longest sub-array with equal number of alphabets and numeric characters
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Longest subsequence of the form 0*1*0* in a binary string
- Longest subsequence with at least one character appearing in every string
- Longest Subsequence of a String containing only vowels
- Longest Subsequence of a String containing only Consonants
- Longest sub-sequence of a binary string divisible by 3
- Split numeric, alphabetic and special symbols from a String
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Python Regex to extract maximum numeric value from a string
- Splitting a Numeric String
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Split a Numeric String into Fibonacci Sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Improved By : offbeat

