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 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 } ) ; 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
using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { String str = "129365" ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int no = 8 ; while ( no < 100 ) { no = no + 8 ; if ( mp . ContainsKey ( ( no / 10 ) % 10 ) ) mp [ ( no / 10 ) % 10 ] = no % 10 ; else mp . Add ( ( no / 10 ) % 10 , no % 10 ) ; } bool [ ] visited = new bool [ 10 ] ; int i ; for ( i = str . Length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '8' ) { Console . Write ( " Yes " ) ; break ; } if ( visited [ mp [ str [ i ] - '0' ] ] ) { Console . Write ( " Yes " ) ; break ; } visited [ str [ i ] - '0' ] = true ; } if ( i == - 1 ) Console . Write ( " No " ) ; } }