Remove characters from a numeric string such that string becomes divisible by 8
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++
#include <bits/stdc++.h>
using namespace std;
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();
}
int getMultiple(string s)
{
for ( int i = 0; i < 1e3; i += 8) {
if (checkSub(to_string(i), s))
return i;
}
return -1;
}
int main()
{
string s = "3454" ;
cout << getMultiple(s);
return 0;
}
|
Java
class GFG
{
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();
}
static int getMultiple(String s)
{
for ( int i = 0 ; i < 1E3; i += 8 )
{
if (checkSub(Integer.toString(i), s))
return i;
}
return - 1 ;
}
public static void main (String[] args)
{
String s = "3454" ;
System.out.println(getMultiple(s));
}
}
|
Python3
import math as mt
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
def getMultiple(s):
for i in range ( 0 , 10 * * 3 , 8 ):
if (checkSub( str (i), s)):
return i
return - 1
s = "3454"
print (getMultiple(s))
|
C#
using System;
class GFG
{
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;
}
static int getMultiple( string s)
{
for ( int i = 0; i < 1e3; i += 8)
{
if (checkSub(i.ToString(), s))
return i;
}
return -1;
}
static void Main()
{
string s = "3454" ;
Console.WriteLine(getMultiple(s));
}
}
|
PHP
<?php
function checkSub( $sub , $s )
{
$j = 0;
for ( $i = 0; $i < strlen ( $s ); $i ++)
if ( $sub [ $j ] == $s [ $i ])
$j ++;
return $j == strlen ( $sub );
}
function getMultiple( $s )
{
for ( $i = 0; $i < 1e3; $i += 8)
{
if (checkSub((string)( $i ), $s ))
return $i ;
}
return -1;
}
$s = "3454" ;
echo getMultiple( $s );
|
Javascript
<script>
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 getMultiple(s)
{
for (let i = 0; i < 1e3; i += 8)
{
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)
Last Updated :
07 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...