Remove characters from a numeric string such that string becomes divisible by 8
Last Updated :
11 Jul, 2025
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.
Steps to solve the problem:
- In a function checkSub that takes two string arguments, sub and s.
- Initialize a variable j to 0.
- Iterate over the characters of s using a for loop from i = 0 to i < s.size().
- Check if the jth character of sub is equal to the ith character of s than increment j by 1.
- if j is equal to the length of sub, return true. Otherwise, return false.
- In a function getMultiple that takes a string argument s.
- Iterate over all multiples of 8 from 0 to 999 using a for loop from i = 0 to i < 1000 by incrementing i by 8.
- Check if the current multiple i exists as a subsequence in the given string s using the checkSub function defined earlier.
- If i exists as a subsequence, return i.
- If no multiple of 8 is found, return -1.
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 <bits/stdc++.h>
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
<?php
// PHP program to remove digits from a
// numeric string such that the number
// becomes divisible by 8
// Function that return true if sub
// is a sub-sequence in s
function checkSub($sub, $s)
{
$j = 0;
for ($i = 0; $i < strlen($s); $i++)
if ($sub[$j] == $s[$i])
$j++;
return $j == strlen($sub);
}
// Function to return a multiple of 8
// formed after removing 0 or more
// characters from the given string
function getMultiple($s)
{
// Iterate over all multiples of 8
for ($i = 0; $i < 1e3; $i += 8)
{
// If current multiple
// exists as a subsequence
// in the given string
if (checkSub((string)($i), $s))
return $i;
}
return -1;
}
// Driver Code
$s = "3454";
echo getMultiple($s);
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// JavaScript program to remove digits from a
// numeric string such that the number
// becomes divisible by 8
// Function that return true if sub
// is a sub-sequence in s
function checkSub(sub, s)
{
let j = 0;
for (let 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
function getMultiple(s)
{
// Iterate over all multiples of 8
for (let 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;
}
let s = "3454";
document.write(getMultiple(s));
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Transform the given String into two same Strings by removing at most one character Given a string S of length N where (N ? 1). The task is to check whether by removing at most one character from the given string it can be divided into two same sub-strings or not return YES or NO for possibilities. Examples: Input: N = 5, S = abzabOutput: YESExplanation: Character 'z' at index 3 ca
9 min read
Maximize minority character deletions that can be done from given Binary String substring Python Given binary string str of size, N. Select any substring from the string and remove all the occurrences of the minority character (i.e. the character having less frequency) from the substring. The task is to find out the maximum number of characters that can be removed from performing one suc
5 min read
Print digit's position to be removed to make a number divisible by 6 Given a number N, remove exactly one digit to make the number divisible by 6 (make it the largest possible). Print the position that has to be removed, If not possible then print -1. Examples: Input: 123 Output: 3 Explanation: Remove 3rd position element and hence the number is 12, which is divisibl
15 min read
Program for removing i-th character from a string Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a
5 min read
Count ways to make the number formed by K concatenations of a numeric string divisible by 5 Given a string S consisting of N digits and an integer K, the task is to count the number of ways to remove digits from the number formed by the concatenating the string S, K number of times, such that the resulting string is divisible by 5. Since count can be very large, so print it to modulo 109 +
8 min read
Longest Subsequence from a numeric String divisible by K 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 = 8Output: 121400Explanation:Since the whole string is divisible by 8, the entire string is the answer. Input: str: "7675437", K
8 min read