Given a non-negative integer represented in the form of a numeric string str. Remove zero or more characters from the string such that the number becomes divisible by 8. If it is possible, print the string after removing the characters otherwise print -1.
Input: str = “3454”
After removing ‘5’, string becomes 344 which is divisible by 8.
Input: str = “111”
Approach: Considering the divisibility rule of 8, we just need to check if the number formed by last 3 characters of str is divisible by 8 or not. Thus, we can iterate over all multiples of 8 upto 1000 and check if any of the multiple exists as a sub-sequence in the given string, then that multiple is our required answer. Otherwise, there exists no answer since all multiples of 8 greater than 1000 also needs to have the number (formed from last 3 digits) which has already been checked.
Below is the implementation of the above approach:
- Remove even frequency characters from the string
- Remove characters from string that appears strictly less than K times
- Remove minimum characters from string to split it into three substrings under given constraints
- Count of times second string can be formed from the characters of first string
- Largest sub-string of a binary string divisible by 2
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Split a Numeric String into Fibonacci Sequence
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- How to get the last n characters of a PHP string?
- How to remove the first character of string in PHP?
- Remove new lines from string in PHP
- Number of subsequences in a string divisible by n
- Check whether the given decoded string is divisible by 6
- Program to remove consonants from a String
- Count of 1-bit and 2-bit characters in the given binary string
- Add index to characters and reverse the string
- Iterate over characters of a string in Python
- Largest sub-string where all the characters appear at least K times
- Number of substrings divisible by 4 in a string of integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.