Divide number into two parts divisible by given numbers | C ++ code to break the number string into two divisible parts 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 base 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 to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwoDivisibleParts ( string num , int f , int s ) { int N = num . length ( ) ; int prefixReminder [ N + 1 ] ; int suffixReminder [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num [ i - 1 ] - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num [ i ] - '0' ) * base ) % s ; base = ( base * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num [ i ] != '0' ) { cout << num . substr ( 0 , i ) << " ▁ " << num . substr ( i ) << endl ; return ; } } cout << " Not ▁ Possible STRNEWLINE " ; } int main ( ) { string num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; return 0 ; }