Partition a number into two divisible parts
Last Updated :
21 Dec, 2022
Given a number (as string) and two integers a and b, divide the string in two non-empty parts such that the first part is divisible by a and the second part is divisible by b. If the string can not be divided into two non-empty parts, output “NO”, else print “YES” with the two parts.
Examples:
Input: str = “123”, a = 12, b = 3
Output: YES
12 3
Explanation: “12” is divisible by a and “3” is divisible by b.
Input: str = “1200”, a = 4, b = 3
Output: YES
12 00
Input: str = “125”, a = 12, b = 3
Output: NO
A simple solution is to one by one partition array around all points. For every partition, check if left and right of it are divisible by a and b respectively. If yes, print the left and right parts and return.
An efficient solution is to do some preprocessing and save the division modulo by ‘a’ by scanning the string from left to right and division modulo by ‘b’ from right to left.
If we know the remainder of prefix from 0 to i, when divided by a, then we compute remainder of prefix from 0 to i+1 using below formula.
lr[i+1] = (lr[i]*10 + str[i] -‘0’)%a.
Same way, modulo by b can be found by scanning from right to left. We create another rl[] to store remainders with b from right to left.
Once we have precomputed two remainders, we can easily find the point that partition string in two parts.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findDivision(string &str, int a, int b)
{
int len = str.length();
vector< int > lr(len+1, 0);
lr[0] = (str[0] - '0' )%a;
for ( int i=1; i<len; i++)
lr[i] = ((lr[i-1]*10)%a + (str[i]- '0' ))%a;
vector< int > rl(len+1, 0);
rl[len-1] = (str[len-1] - '0' )%b;
int power10 = 10;
for ( int i= len-2; i>=0; i--)
{
rl[i] = (rl[i+1] + (str[i]- '0' )*power10)%b;
power10 = (power10 * 10) % b;
}
for ( int i=0; i<len-1; i++)
{
if (lr[i] != 0)
continue ;
if (rl[i+1] == 0)
{
cout << "YES\n" ;
for ( int k=0; k<=i; k++)
cout << str[k];
cout << ", " ;
for ( int k=i+1; k<len; k++)
cout << str[k];
return ;
}
}
cout << "NO\n" ;
}
int main()
{
string str = "123" ;
int a = 12, b = 3;
findDivision(str, a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findDivision(String str, int a, int b)
{
int len = str.length();
int [] lr = new int [len + 1 ];
lr[ 0 ] = (( int )str.charAt( 0 ) - ( int ) '0' )%a;
for ( int i = 1 ; i < len; i++)
lr[i] = ((lr[i - 1 ] * 10 ) % a +
(( int )str.charAt(i)-( int ) '0' )) % a;
int [] rl = new int [len + 1 ];
rl[len - 1 ] = (( int )str.charAt(len - 1 ) -
( int ) '0' ) % b;
int power10 = 10 ;
for ( int i= len - 2 ; i >= 0 ; i--)
{
rl[i] = (rl[i + 1 ] + (( int )str.charAt(i) -
( int ) '0' ) * power10) % b;
power10 = (power10 * 10 ) % b;
}
for ( int i = 0 ; i < len - 1 ; i++)
{
if (lr[i] != 0 )
continue ;
if (rl[i + 1 ] == 0 )
{
System.out.println( "YES" );
for ( int k = 0 ; k <= i; k++)
System.out.print(str.charAt(k));
System.out.print( ", " );
for ( int k = i + 1 ; k < len; k++)
System.out.print(str.charAt(k));
return ;
}
}
System.out.println( "NO" );
}
public static void main (String[] args)
{
String str = "123" ;
int a = 12 , b = 3 ;
findDivision(str, a, b);
}
}
|
Python3
def findDivision( str , a, b):
lenn = len ( str )
lr = [ 0 ] * (lenn + 1 )
lr[ 0 ] = ( int ( str [ 0 ])) % a
for i in range ( 1 , lenn):
lr[i] = ((lr[i - 1 ] * 10 ) % a + \
int ( str [i])) % a
rl = [ 0 ] * (lenn + 1 )
rl[lenn - 1 ] = int ( str [lenn - 1 ]) % b
power10 = 10
for i in range (lenn - 2 , - 1 , - 1 ):
rl[i] = (rl[i + 1 ] + int ( str [i]) * power10) % b
power10 = (power10 * 10 ) % b
for i in range ( 0 , lenn - 1 ):
if (lr[i] ! = 0 ):
continue
if (rl[i + 1 ] = = 0 ):
print ( "YES" )
for k in range ( 0 , i + 1 ):
print ( str [k], end = "")
print ( "," , end = " " )
for i in range (i + 1 , lenn):
print ( str [k], end = "")
return
print ( "NO" )
str = "123"
a, b = 12 , 3
findDivision( str , a, b)
|
C#
using System;
class GFG
{
static void findDivision( string str, int a, int b)
{
int len = str.Length;
int [] lr = new int [len + 1];
lr[0] = (( int )str[0] - ( int ) '0' )%a;
for ( int i = 1; i < len; i++)
lr[i] = ((lr[i - 1] * 10) % a +
(( int )str[i] - ( int ) '0' )) % a;
int [] rl = new int [len + 1];
rl[len - 1] = (( int )str[len - 1] - ( int ) '0' ) % b;
int power10 = 10;
for ( int i= len - 2; i >= 0; i--)
{
rl[i] = (rl[i + 1] + (( int )str[i] -
( int ) '0' ) * power10) % b;
power10 = (power10 * 10) % b;
}
for ( int i = 0; i < len - 1; i++)
{
if (lr[i] != 0)
continue ;
if (rl[i + 1] == 0)
{
Console.WriteLine( "YES" );
for ( int k = 0; k <= i; k++)
Console.Write(str[k]);
Console.Write( ", " );
for ( int k = i + 1; k < len; k++)
Console.Write(str[k]);
return ;
}
}
Console.WriteLine( "NO" );
}
static void Main()
{
string str = "123" ;
int a = 12, b = 3;
findDivision(str, a, b);
}
}
|
Javascript
<script>
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] - '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( "YES<br>" );
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( "NO<br>" );
}
let str = "123" ;
let a = 12, b = 3;
findDivision(str, a, b);
</script>
|
Time Complexity: O(n) where n is the length of input number string.
Auxiliary Space: O(n)
Another approach: (Using built-in function)
This problem can also be solved using built-in library functions to convert string to integer and integer to string.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
string findDivision(string S, int a, int b)
{
for ( int i = 0; i < S.size() - 1; i++) {
string firstPart = S.substr(0, i + 1);
string secondPart = S.substr(i + 1);
if (stoi(firstPart) % a == 0
and stoi(secondPart) % b == 0)
return firstPart + " " + secondPart;
}
return "-1" ;
}
int main()
{
string str = "125" ;
int a = 12, b = 3;
string result = findDivision(str, a, b);
if (result == "-1" ) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
cout << result << endl;
}
return 0;
}
|
Java
import java.io.*;
public class GFG
{
public static String findDivision(String S, int a,
int b)
{
for ( int i = 0 ; i < S.length() - 1 ; i++) {
String firstPart = S.substring( 0 , i + 1 );
String secondPart = S.substring(i + 1 );
if (Integer.parseInt(firstPart) % a == 0
&& Integer.parseInt(secondPart) % b == 0 ) {
return firstPart + " " + secondPart;
}
}
return "-1" ;
}
public static void main(String[] args)
{
String str = "125" ;
int a = 12 ;
int b = 3 ;
String result = findDivision(str, a, b);
if (result.equals( "-1" )) {
System.out.print( "NO" );
System.out.print( "\n" );
}
else {
System.out.print( "YES" );
System.out.print( "\n" );
System.out.print(result);
System.out.print( "\n" );
}
}
}
|
Python3
def findDivision(S, a, b):
for i in range ( len (S) - 1 ):
firstPart = S[ 0 : i + 1 ]
secondPart = S[i + 1 :]
if ( int (firstPart) % a = = 0
and int (secondPart) % b = = 0 ):
return firstPart + " " + secondPart
return "-1"
Str = "125"
a,b = 12 , 3
result = findDivision( Str , a, b)
if (result = = "-1" ):
print ( "NO" )
else :
print ( "YES" )
print (result)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static string findDivision( string S, int a,
int b)
{
for ( int i = 0; i < S.Length - 1; i++) {
string firstPart = S.Substring(0, i + 1);
string secondPart = S.Substring(i + 1);
if (Convert.ToInt32(firstPart) % a == 0
&& Convert.ToInt32(secondPart) % b == 0) {
return firstPart + " " + secondPart;
}
}
return "-1" ;
}
public static void Main( string [] args)
{
string str = "125" ;
int a = 12;
int b = 3;
string result = findDivision(str, a, b);
if (result.Equals( "-1" )) {
Console.WriteLine( "NO" );
}
else {
Console.WriteLine( "YES" );
Console.WriteLine(result);
}
}
}
|
Javascript
<script>
function findDivision(S, a, b){
for (let i=0;i<S.length-1;i++){
let firstPart = S.substring(0,i + 1)
let secondPart = S.substring(i + 1)
if (parseInt(firstPart) % a == 0
&& parseInt(secondPart) % b == 0)
return firstPart + " " + secondPart
}
return "-1"
}
let Str = "125"
let a = 12,b = 3
let result = findDivision(Str, a, b)
if (result == "-1" )
document.write( "NO" , "</br>" )
else {
document.write( "YES" , "</br>" )
document.write(result, "</br>" )
}
</script>
|
Time Complexity: O(n) where n is the length of input number string.
Auxiliary Space: O(1)
Another approach: (Without Using built-in function)
Time Complexity: O(n) where n is the length of input number string.
Auxiliary Space: O(1)
C++
#include <bits/stdc++.h>
using namespace std;
string stringPartition(string s, int a, int b)
{
int n = s.length();
if (n == 1) {
return "-1" ;
}
else {
int a1 = s[0] - '0' ;
int a2 = s[1] - '0' ;
int multiplier = 10;
for ( int i = 2; i < n; i++) {
a2 = a2 * multiplier + (s[i] - '0' );
}
int i = 1;
if (a1 % a == 0 && a2 % b == 0) {
string k1 = string(1, s[0]);
string k2 = "" ;
for ( int j = 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
int q1 = 10;
int q2 = 1;
for ( int i = 1; i < n - 1; i++)
q2 *= 10;
while (i < n - 1) {
char x = s[i];
int ad = x - '0' ;
a1 = a1 * q1 + ad;
a2 = a2 - q2 * ad;
if (a1 % a == 0 && a2 % b == 0) {
string k1 = "" ;
string k2 = "" ;
for ( int j = 0; j < i + 1; j++)
k1 += s[j];
for ( int j = i + 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
q2 /= 10;
i++;
}
}
return "-1" ;
}
int main()
{
string str = "123" ;
int a = 12, b = 3;
string result = stringPartition(str, a, b);
if (result == "-1" ) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
cout << result << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String StringPartition(String s, int a, int b)
{
int i;
int n = s.length();
if (n == 1 ) {
return "-1" ;
}
else {
int a1 = s.charAt( 0 ) - '0' ;
int a2 = s.charAt( 1 ) - '0' ;
int multiplier = 10 ;
for (i = 2 ; i < n; i++) {
a2 = a2 * multiplier + (s.charAt(i) - '0' );
}
i = 1 ;
if (a1 % a == 0 && a2 % b == 0 ) {
String k1 = "" ;
for (i = 0 ; i < s.charAt( 0 ); i++)
k1 += '1' ;
String k2 = "" ;
for ( int j = 1 ; j < n; j++)
k2 += s.charAt(j);
return k1 + " " + k2;
}
int q1 = 10 ;
int q2 = 1 ;
for (i = 1 ; i < n - 1 ; i++)
q2 *= 10 ;
i = 1 ;
while (i < n - 1 ) {
char x = s.charAt(i);
int ad = x - '0' ;
a1 = a1 * q1 + ad;
a2 = a2 - q2 * ad;
if (a1 % a == 0 && a2 % b == 0 ) {
String k1 = "" ;
String k2 = "" ;
for ( int j = 0 ; j < i + 1 ; j++)
k1 += s.charAt(j);
for ( int j = i + 1 ; j < n; j++)
k2 += s.charAt(j);
return k1 + " " + k2;
}
q2 /= 10 ;
i++;
}
}
return "-1" ;
}
public static void main(String[] args)
{
String str = "123" ;
int a = 12 , b = 3 ;
String result = StringPartition(str, a, b);
if (result == "-1" ) {
System.out.println( "NO" );
}
else {
System.out.println( "YES" );
System.out.println(result);
}
}
}
|
Python3
def stringPartition(s, a, b):
n = len (s)
if (n = = 1 ):
return "-1"
else :
a1 = int (s[ 0 ])
a2 = int (s[ 1 ])
multiplier = 10
for i in range ( 2 , n):
a2 = a2 * multiplier + int (s[i])
i = 1
if (a1 % a = = 0 and a2 % b = = 0 ):
k1 = '1' * (s[ 0 ])
k2 = ""
for j in range ( 1 , n):
k2 + = s[j]
return k1 + " " + k2
q1 = 10
q2 = 1
for i in range ( 1 , n - 1 ):
q2 * = 10
while (i < n - 1 ):
x = s[i]
ad = int (x)
a1 = a1 * q1 + ad
a2 = a2 - q2 * ad
if (a1 % a = = 0 and a2 % b = = 0 ):
k1 = ""
k2 = ""
for j in range (i + 1 ):
k1 + = s[j]
for j in range (i + 1 , n):
k2 + = s[j]
return k1 + " " + k2
q2 / / = 10
i + = 1
return "-1"
str = "123"
a = 12
b = 3
result = stringPartition( str , a, b)
if (result = = "-1" ):
print ( "NO" )
else :
print ( "YES" )
print (result)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static string stringPartition( string s, int a, int b)
{
int i;
int n = s.Length;
if (n == 1) {
return "-1" ;
}
else {
int a1 = s[0] - '0' ;
int a2 = s[1] - '0' ;
int multiplier = 10;
for (i = 2; i < n; i++) {
a2 = a2 * multiplier + (s[i] - '0' );
}
i = 1;
if (a1 % a == 0 && a2 % b == 0) {
string k1 = new string ( '1' , s[0]);
string k2 = "" ;
for ( int j = 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
int q1 = 10;
int q2 = 1;
for (i = 1; i < n - 1; i++)
q2 *= 10;
i = 1;
while (i < n - 1) {
char x = s[i];
int ad = x - '0' ;
a1 = a1 * q1 + ad;
a2 = a2 - q2 * ad;
if (a1 % a == 0 && a2 % b == 0) {
string k1 = "" ;
string k2 = "" ;
for ( int j = 0; j < i + 1; j++)
k1 += s[j];
for ( int j = i + 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
q2 /= 10;
i++;
}
}
return "-1" ;
}
public static void Main( string [] args)
{
string str = "123" ;
int a = 12, b = 3;
string result = stringPartition(str, a, b);
if (result == "-1" ) {
Console.WriteLine( "NO" );
}
else {
Console.WriteLine( "YES" );
Console.WriteLine(result);
}
}
}
|
Javascript
function stringPartition(s, a, b)
{
let n = s.length;
if (n == 1) {
return "-1" ;
}
else {
let a1 = parseInt(s[0]);
let a2 = parseInt(s[1]);
let multiplier = 10;
for (let i = 2; i < n; i++) {
a2 = a2 * multiplier + parseInt(s[i]);
}
let i = 1;
if (a1 % a == 0 && a2 % b == 0) {
let k1 = '1' .repeat(s[0]);
let k2 = "" ;
for (let j = 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
let q1 = 10;
let q2 = 1;
for (let i = 1; i < n - 1; i++)
q2 *= 10;
while (i < n - 1) {
let x = s[i];
let ad = parseInt(x);
a1 = a1 * q1 + ad;
a2 = a2 - q2 * ad;
if (a1 % a == 0 && a2 % b == 0) {
let k1 = "" ;
let k2 = "" ;
for (let j = 0; j < i + 1; j++)
k1 += s[j];
for (let j = i + 1; j < n; j++)
k2 += s[j];
return k1 + " " + k2;
}
q2 = Math.floor(10);
i++;
}
}
return "-1" ;
}
let str = "123" ;
let a = 12;
let b = 3;
let result = stringPartition(str, a, b);
if (result == "-1" ) {
console.log( "NO" );
}
else {
console.log( "YES" );
console.log(result);
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...