Divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base1 value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned under constraints ; Driver Code
function printTwoDivisibleParts ( num , f , s ) { var N = num . length ; var prefixReminder = [ ] var suffixReminder = [ ] suffixReminder [ 0 ] = 0 ; for ( var i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num [ i - 1 ] - ' ' ) ) % f ; prefixReminder [ N ] = 0 ; var base1 = 1 ; for ( var i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num [ i ] - ' ' ) * base1 ) % s ; base1 = ( base1 * 10 ) % s ; } for ( var i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num [ i ] != ' ' ) { document . write ( num . substring ( 0 , i ) + " " + num . substring ( i ) ) ; return ; } } document . write ( " " ) ; } var num = " " ; var f = 12345 ; var s = 1024 ; printTwoDivisibleParts ( num , f , s ) ;