Given a large number , check if a subsequence of digits is divisible by 8 | Driver code ; 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 . add ( { key , value } ) if ( mp . containsKey ( ( no / 10 ) % 10 ) ) ; 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
let str = " " ; let mp = new Map ( ) ; let no = 8 ; while ( no < 100 ) { no = no + 8 ; mp . set ( ( Math . floor ( no / 10 ) ) % 10 , no % 10 ) ; } let visited = new Array ( 10 ) ; for ( let i = 0 ; i < visited . length ; i ++ ) { visited [ i ] = false ; } let i ; for ( i = str . length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) { document . write ( " " ) ; break ; } if ( visited [ mp . get ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ] ) { document . write ( " " ) ; break ; } visited [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = true ; } if ( i == - 1 ) document . write ( " " ) ;