Converting string to integer array for ease of computations ( Indexing in arr [ ] is considered to be starting from 1 )
$ arr = array_fill ( 0 , ( $ n + 1 ) , NULL ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ arr [ $ i ] = $ str [ $ i - 1 ] - '0' ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ dp [ $ i ] [ $ arr [ $ i ] % 8 ] = 1 ; for ( $ j = 0 ; $ j < 8 ; $ j ++ ) {