Related Articles

# Remove characters from a numeric string such that string becomes divisible by 8

• Difficulty Level : Medium
• Last Updated : 01 Jun, 2021

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.
Examples:

Input: str = “3454”
Output: 344
After removing ‘5’, string becomes 344 which is divisible by 8.
Input: str = “111”
Output: -1

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:

## C++

 `// C++ program to remove digits from a``// numeric string such that the number``// becomes divisible by 8``#include ``using` `namespace` `std;` `// Function that return true if sub``// is a sub-sequence in s``int` `checkSub(string sub, string s)``{``    ``int` `j = 0;``    ``for` `(``int` `i = 0; i < s.size(); i++)``        ``if` `(sub[j] == s[i])``            ``j++;``    ``return` `j == (``int``)sub.size();``}` `// Function to return a multiple of 8``// formed after removing 0 or more characters``// from the given string``int` `getMultiple(string s)``{``    ``// Iterate over all multiples of 8``    ``for` `(``int` `i = 0; i < 1e3; i += 8) {` `        ``// If current multiple``        ``// exists as a subsequence``        ``// in the given string``        ``if` `(checkSub(to_string(i), s))``            ``return` `i;``    ``}` `    ``return` `-1;``}` `// Driver Code``int` `main()``{``    ``string s = ``"3454"``;``    ``cout << getMultiple(s);` `    ``return` `0;``}`

## Java

 `// Java program to remove digits from a``// numeric string such that the number``// becomes divisible by 8` `class` `GFG``{` `    ``// Function that return true if sub``    ``// is a sub-sequence in s``    ``static` `boolean` `checkSub(String sub, String s)``    ``{``        ``int` `j = ``0``;``        ``for` `(``int` `i = ``0``; i < s.length(); i++)``            ``if` `(sub.charAt(j) == s.charAt(i))``                ``j++;``                ` `        ``return` `j == sub.length();``    ``}``    ` `    ``// Function to return a multiple of 8``    ``// formed after removing 0 or more characters``    ``// from the given string``    ``static` `int` `getMultiple(String s)``    ``{``        ``// Iterate over all multiples of 8``        ``for` `(``int` `i = ``0``; i < 1E3; i += ``8``)``        ``{``    ` `            ``// If current multiple``            ``// exists as a subsequence``            ``// in the given string``            ``if` `(checkSub(Integer.toString(i), s))``                ``return` `i;``        ``}``        ``return` `-``1``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main (String[] args)``    ``{``            ``String s = ``"3454"``;``            ``System.out.println(getMultiple(s));``    ``}``}` `// This code is contributed by mits`

## Python3

 `# Python3 program to remove digits from``# a numeric string such that the number``# becomes divisible by 8``import` `math as mt` `# Function that return true if sub``# is a sub-sequence in s``def` `checkSub(sub, s):` `    ``j ``=` `0``    ``for` `i ``in` `range``(``len``(s)):``        ``if` `(sub[j] ``=``=` `s[i]):``            ``j ``+``=` `1` `    ``if` `j ``=``=` `int``(``len``(sub)):``        ``return` `True``    ``else``:``        ``return` `False` `# Function to return a multiple of 8``# formed after removing 0 or more``# characters from the given string``def` `getMultiple(s):``    ` `    ``# Iterate over all multiples of 8``    ``for` `i ``in` `range``(``0``, ``10``*``*``3``, ``8``):` `        ``# If current multiple``        ``# exists as a subsequence``        ``# in the given string``        ``if` `(checkSub(``str``(i), s)):``            ``return` `i` `    ``return` `-``1` `# Driver Code``s ``=` `"3454"``print``(getMultiple(s))` `# This code is contributed``# by Mohit Kumar 29`

## C#

 `// C# program to remove digits from a``// numeric string such that the number``// becomes divisible by 8``using` `System;` `class` `GFG``{` `    ``// Function that return true if sub``    ``// is a sub-sequence in s``    ``static` `bool` `checkSub(``string` `sub, ``string` `s)``    ``{``        ``int` `j = 0;``        ``for` `(``int` `i = 0; i < s.Length; i++)``            ``if` `(sub[j] == s[i])``                ``j++;``                ` `        ``return` `j == sub.Length;``    ``}``    ` `    ``// Function to return a multiple of 8``    ``// formed after removing 0 or more characters``    ``// from the given string``    ``static` `int` `getMultiple(``string` `s)``    ``{``        ``// Iterate over all multiples of 8``        ``for` `(``int` `i = 0; i < 1e3; i += 8)``        ``{``    ` `            ``// If current multiple``            ``// exists as a subsequence``            ``// in the given string``            ``if` `(checkSub(i.ToString(), s))``                ``return` `i;``        ``}``        ``return` `-1;``    ``}` `    ``// Driver Code``    ``static` `void` `Main()``    ``{``            ``string` `s = ``"3454"``;``            ``Console.WriteLine(getMultiple(s));``    ``}``}` `// This code is contributed by Ryuga`

## PHP

 `

## Javascript

 ``
Output:
`344` My Personal Notes arrow_drop_up