Given a large number , check if a subsequence of digits is divisible by 8 | C ++ program to check if given string has a subsequence divisible by 8 ; Driver function ; map key will be tens place digit of number that is divisible by 8 and value will be units place digit ; For filling the map let start with initial value 8 ; key is digit at tens place and value is digit at units place mp . insert ( { key , value } ) ; Create a hash to check if we visited a number ; Iterate from last index to 0 th index ; If 8 is present in string then 8 divided 8 hence print yes ; considering present character as the second digit of two digits no we check if the value of this key is marked in hash or not If marked then we a have a number divisible by 8 ; If no subsequence divisible by 8
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { string str = "129365" ; map < int , int > mp ; int no = 8 ; while ( no < 100 ) { no = no + 8 ; mp . insert ( { ( no / 10 ) % 10 , no % 10 } ) ; } vector < bool > visited ( 10 , false ) ; int i ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '8' ) { cout << " Yes " ; break ; } if ( visited [ mp [ str [ i ] - '0' ] ] ) { cout << " Yes " ; break ; } visited [ str [ i ] - '0' ] = true ; } if ( i == -1 ) cout << " No " ; return 0 ; }