Partition a number into two divisible parts | Finds if it is possible to partition str into two parts such that first part is divisible by a and second part is divisible by b . ; Create an array of size len + 1 and initialize it with 0. Store remainders from left to right when divided by ' a ' ; Compute remainders from right to left when divided by ' b ' ; Find a point that can partition a number ; If split is not possible at this point ; We can split at i if one of the following two is true . a ) All characters after str [ i ] are 0 b ) String after str [ i ] is divisible by b , i . e . , str [ i + 1. . n - 1 ] is divisible by b . ; Driver code
function findDivision ( str , a , b ) { let len = str . length ; let lr = [ ] ; for ( let i = 0 ; i < len + 1 ; i ++ ) lr . push ( 0 ) ; lr [ 0 ] = ( str [ 0 ] - ' ' ) % a ; for ( let i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( str . charCodeAt ( i ) ) ) % a ; let rl = [ ] ; for ( let i = 0 ; i < len + 1 ; i ++ ) rl . push ( 0 ) ; rl [ len - 1 ] = ( str . charCodeAt ( len - 1 ) ) % b ; let power10 = 10 ; for ( let i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( str . charCodeAt ( i ) ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( let i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { document . write ( " " ) ; for ( let k = 0 ; k <= i ; k ++ ) document . write ( str [ k ] ) ; document . write ( " " ) ; for ( let k = i + 1 ; k < len ; k ++ ) document . write ( str [ k ] ) ; return ; } } document . write ( " " ) ; } let str = " " ; let a = 12 , b = 3 ; findDivision ( str , a , b ) ;