Given a large number , check if a subsequence of digits is divisible by 8 | Java program to check if given String has a subsequence 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
import java . util . * ; class GFG { public static void main ( String [ ] args ) { String str = "129365" ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int no = 8 ; while ( no < 100 ) { no = no + 8 ; mp . put ( ( no / 10 ) % 10 , no % 10 ) ; } boolean [ ] visited = new boolean [ 10 ] ; int i ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '8' ) { System . out . print ( " Yes " ) ; break ; } if ( visited [ mp . get ( str . charAt ( i ) - '0' ) ] ) { System . out . print ( " Yes " ) ; break ; } visited [ str . charAt ( i ) - '0' ] = true ; } if ( i == - 1 ) System . out . print ( " No " ) ; } }