Remove characters from a numeric string such that string becomes divisible by 8 | Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code
< ? 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 ) ;