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
function checkSub ( sub , s ) { let j = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) if ( sub [ j ] == s [ i ] ) j ++ ; return j == sub . length ; } function getMultiple ( s ) { for ( let i = 0 ; i < 1e3 ; i += 8 ) { if ( checkSub ( i . toString ( ) , s ) ) return i ; } return - 1 ; } let s = " " ; document . write ( getMultiple ( s ) ) ;