# Partition a number into two divisible parts

• Difficulty Level : Hard
• Last Updated : 29 Aug, 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++

 `// C++ program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``#include ``using` `namespace` `std;` `// 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.``void` `findDivision(string &str, ``int` `a, ``int` `b)``{``    ``int` `len = str.length();` `    ``// Create an array of size len+1 and initialize``    ``// it with 0.``    ``// Store remainders from left to right when``    ``// divided by 'a'``    ``vector<``int``> lr(len+1, 0);``    ``lr = (str - ``'0'``)%a;``    ``for` `(``int` `i=1; i 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;``    ``}` `    ``// Find a point that can partition a number``    ``for` `(``int` `i=0; i

## Java

 `// Java program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``class` `GFG``{``    ` `// 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.``static` `void` `findDivision(String str, ``int` `a, ``int` `b)``{``    ``int` `len = str.length();` `    ``// Create an array of size len+1 and initialize``    ``// it with 0.``    ``// Store remainders from left to right when``    ``// divided by 'a'``    ``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;` `    ``// Compute remainders from right to left when``    ``// divided by 'b'``    ``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;``    ``}` `    ``// Find a point that can partition a number``    ``for` `(``int` `i = ``0``; i < len - ``1``; i++)``    ``{``        ``// If split is not possible at this point``        ``if` `(lr[i] != ``0``)``            ``continue``;` `        ``// We can split at i if one of the following``        ``// two is true.``        ``// a) All characters after str.charAt(i] are 0``        ``// b) String after str.charAt(i] is divisible by b, i.e.,``        ``// str.charAt(i+1..n-1] is divisible by b.``        ``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"``);``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``String str = ``"123"``;``    ``int` `a = ``12``, b = ``3``;``    ``findDivision(str, a, b);``}``}` `// This code is contributed by mits`

## Python3

 `# Python3 program to check if a can be splitted``# into two strings such that one is divisible by 'a'``# and other is divisible by 'b'.` `# 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.``def` `findDivision(``str``, a, b):``    ``lenn ``=` `len``(``str``)``    ` `    ``# Create an array of size lenn+1 and``    ``# initialize it with 0.``    ``# Store remainders from left to right``    ``# when divided by 'a'``    ``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``                     ` `    ``# Compute remainders from right to left``    ``# when divided by 'b'``    ``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``        ` `    ``# Find a point that can partition a number``    ``for` `i ``in` `range``(``0``, lenn ``-` `1``):``        ` `        ``# If split is not possible at this point``        ``if` `(lr[i] !``=` `0``):``            ``continue``            ` `        ``# We can split at i if one of the following``        ``# two is true.``        ``# a) All characters after str[i] are 0``        ``# b) after str[i] is divisible by b, i.e.,``        ``# str[i+1..n-1] is divisible by b.``        ``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"``)` `# Driver code``str` `=` `"123"``a, b ``=` `12``, ``3``findDivision(``str``, a, b)` `# This code is contributed by SHUBHAMSINGH10`

## C#

 `// C# program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``using` `System;` `class` `GFG``{``    ` `// 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.``static` `void` `findDivision(``string` `str, ``int` `a, ``int` `b)``{``    ``int` `len = str.Length;` `    ``// Create an array of size len+1 and initialize``    ``// it with 0.``    ``// Store remainders from left to right when``    ``// divided by 'a'``    ``int``[] lr = ``new` `int``[len + 1];``    ``lr = ((``int``)str - (``int``)``'0'``)%a;``    ` `    ``for` `(``int` `i = 1; i < len; i++)``        ``lr[i] = ((lr[i - 1] * 10) % a +``                ``((``int``)str[i] - (``int``)``'0'``)) % a;` `    ``// Compute remainders from right to left when``    ``// divided by 'b'``    ``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;``    ``}` `    ``// Find a point that can partition a number``    ``for` `(``int` `i = 0; i < len - 1; i++)``    ``{``        ``// If split is not possible at this point``        ``if` `(lr[i] != 0)``            ``continue``;` `        ``// 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.``        ``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"``);``}` `// Driver code``static` `void` `Main()``{``    ``string` `str = ``"123"``;``    ``int` `a = 12, b = 3;``    ``findDivision(str, a, b);``}``}` `// This code is contributed by mits`

## Javascript

 ``

Output

```YES
12, 3```

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++

 `// C++ program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``#include ``using` `namespace` `std;` `// 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.``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"``;``}` `// Driver code``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;``}` `// This code is contributed by Ishan Khandelwal`

## Java

 `// Java program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``public` `class` `GFG``{` `  ``// 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.``  ``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"``;``  ``}` `  ``// Driver code``  ``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"``);``    ``}``  ``}``}` `// This code is contributed by Aarti_Rathi`

## Python3

 `# Python program to check if a string can be splitted``# into two strings such that one is divisible by 'a'``# and other is divisible by 'b'.` `# 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.``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"` `# Driver code``Str` `=` `"125"``a,b ``=` `12``,``3``result ``=` `findDivision(``Str``, a, b)` `if` `(result ``=``=` `"-1"``):``    ``print``(``"NO"``)` `else``:``    ``print``(``"YES"``)``    ``print``(result)` `# This code is contributed by shinjanpatra`

## C#

 `// C# program to check if a string can be splitted``// into two strings such that one is divisible by 'a'``// and other is divisible by 'b'.``using` `System;``using` `System.Collections.Generic;` `public` `class` `GFG {` `  ``// 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.``  ``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"``;``  ``}` `  ``// Driver code``  ``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);``    ``}``  ``}``}` `// This code is contributed by phasing17`

## Javascript

 ``

Output

`NO`

Time Complexity: O(n) where n is the length of input number string.
Auxiliary Space: O(1)

This article is contributed by Ekta Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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 ``using` `namespace` `std;``// This code kind of uses sliding window technique. First``// checking if string and string[0..n-1] is divisible if``// yes then return else run a loop from 1 to n-1 and check if``// taking this (0-i)index number and (i+1 to n-1)index number``// on our two declared variables if they are divisible by given two numbers respectively``// in any iteration return them simply``string stringPartition(string s, ``int` `a, ``int` `b)``{``    ``// code here``    ``int` `n = s.length();``  ``// if length is 1 not possible``    ``if` `(n == 1) {``        ``return` `"-1"``;``    ``}``    ``else` `{``      ``// Checking if number formed bt S and S[1->n-1] is divisible``        ``int` `a1 = s - ``'0'``;``        ``int` `a2 = s - ``'0'``;``        ``int` `multiplyer = 10;``        ``for` `(``int` `i = 2; i < n; i++) {``            ``a2 = a2 * multiplyer + (s[i] - ``'0'``);``        ``}``        ``int` `i = 1;``        ``if` `(a1 % a == 0 && a2 % b == 0) {``            ``string k1 = string(1, s);``            ``string k2 = ``""``;``            ``for` `(``int` `j = 1; j < n; j++)``                ``k2 += s[j];``            ``return` `k1 + ``" "` `+ k2; ``// return the numbers formed as string``        ``}``      ``// from here by using sliding window technique we will iterate and check for every i``      ``// that if the two current numbers formed are divisible if yes return``      ``// else form the two new numbers for next iteration using sliding window technique``        ``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"``;``}``// Driver code``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;``}``// This code is contributed by Kartikey Singh`

## Java

 `// Java code to implement the approach` `import` `java.util.*;` `class` `GFG``{` `  ``// This code kind of uses sliding window technique.``  ``// First checking if String and String[0..n-1] is``  ``// divisible if yes then return else run a loop from 1``  ``// to n-1 and check if taking this (0-i)index number and``  ``// (i+1 to n-1)index number on our two declared``  ``// variables if they are divisible by given two numbers``  ``// respectively in any iteration return them simply``  ``static` `String StringPartition(String s, ``int` `a, ``int` `b)``  ``{``    ``int` `i;``    ``// code here``    ``int` `n = s.length();``    ``// if length is 1 not possible``    ``if` `(n == ``1``) {``      ``return` `"-1"``;``    ``}``    ``else` `{``      ``// Checking if number formed bt S and``      ``// S[1->n-1] is divisible``      ``int` `a1 = s.charAt(``0``) - ``'0'``;``      ``int` `a2 = s.charAt(``1``) - ``'0'``;``      ``int` `multiplyer = ``10``;``      ``for` `(i = ``2``; i < n; i++) {``        ``a2 = a2 * multiplyer + (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; ``// return the numbers``        ``// formed as String``      ``}` `      ``// from here by using sliding window technique``      ``// we will iterate and check for every i that if``      ``// the two current numbers formed are divisible``      ``// if yes return else form the two new numbers``      ``// for next iteration using sliding window``      ``// technique``      ``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"``;``  ``}` `  ``// Driver code``  ``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);``    ``}``  ``}``}` `// This code is contributed by phasing17`

## Python3

 `# Python3 code to implement the approach` `# This code kind of uses sliding window technique. First``# checking if string and string[0..n-1] is divisible if``# yes then return else run a loop from 1 to n-1 and check if``# taking this (0-i)index number and (i+1 to n-1)index number``# on our two declared variables if they are divisible by given two numbers respectively``# in any iteration return them simply``def` `stringPartition(s, a, b):``    ``# code here``    ``n ``=` `len``(s)` `    ``# if length is 1 not possible``    ``if` `(n ``=``=` `1``):``        ``return` `"-1"` `    ``else``:``        ``# Checking if number formed bt S and S[1->n-1] is divisible``        ``a1 ``=` `int``(s[``0``])``        ``a2 ``=` `int``(s[``1``])``        ``multiplyer ``=` `10``        ``for` `i ``in` `range``(``2``, n):``            ``a2 ``=` `a2 ``*` `multiplyer ``+` `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   ``# return the numbers formed as string` `        ``# from here by using sliding window technique we``        ``# will iterate and check for every i that if the``        ``# two current numbers formed are divisible if yes``        ``# return else form the two new numbers for next``        ``# iteration using sliding window technique``        ``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"` `# Driver code``str` `=` `"123"``a ``=` `12``b ``=` `3``result ``=` `stringPartition(``str``, a, b)` `if` `(result ``=``=` `"-1"``):``    ``print``(``"NO"``)``else``:``    ``print``(``"YES"``)``    ``print``(result)` `# This code is contributed by phasing17`

## C#

 `// C# code to implement the approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `  ``// This code kind of uses sliding window technique.``  ``// First checking if string and string[0..n-1] is``  ``// divisible if yes then return else run a loop from 1``  ``// to n-1 and check if taking this (0-i)index number and``  ``// (i+1 to n-1)index number on our two declared``  ``// variables if they are divisible by given two numbers``  ``// respectively in any iteration return them simply``  ``static` `string` `stringPartition(``string` `s, ``int` `a, ``int` `b)``  ``{``    ``int` `i;``    ``// code here``    ``int` `n = s.Length;``    ``// if length is 1 not possible``    ``if` `(n == 1) {``      ``return` `"-1"``;``    ``}``    ``else` `{``      ``// Checking if number formed bt S and``      ``// S[1->n-1] is divisible``      ``int` `a1 = s - ``'0'``;``      ``int` `a2 = s - ``'0'``;``      ``int` `multiplyer = 10;``      ``for` `(i = 2; i < n; i++) {``        ``a2 = a2 * multiplyer + (s[i] - ``'0'``);``      ``}``      ``i = 1;``      ``if` `(a1 % a == 0 && a2 % b == 0) {``        ``string` `k1 = ``new` `string``(``'1'``, s);``        ``string` `k2 = ``""``;``        ``for` `(``int` `j = 1; j < n; j++)``          ``k2 += s[j];``        ``return` `k1 + ``" "` `+ k2; ``// return the numbers``        ``// formed as string``      ``}` `      ``// from here by using sliding window technique``      ``// we will iterate and check for every i that if``      ``// the two current numbers formed are divisible``      ``// if yes return else form the two new numbers``      ``// for next iteration using sliding window``      ``// technique``      ``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"``;``  ``}` `  ``// Driver code``  ``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);``    ``}``  ``}``}` `// This code is contributed by phasing17`

## Javascript

 `// JavaScript code to implement the approach`  `// This code kind of uses sliding window technique. First``// checking if string and string[0..n-1] is divisible if``// yes then return else run a loop from 1 to n-1 and check if``// taking this (0-i)index number and (i+1 to n-1)index number``// on our two declared variables if they are divisible by given two numbers respectively``// in any iteration return them simply``function` `stringPartition(s, a, b)``{``    ``// code here``    ``let n = s.length;``    ` `  ``// if length is 1 not possible``    ``if` `(n == 1) {``        ``return` `"-1"``;``    ``}``    ``else` `{``      ``// Checking if number formed bt S and S[1->n-1] is divisible``        ``let a1 = parseInt(s);``        ``let a2 = parseInt(s);``        ``let multiplyer = 10;``        ``for` `(let i = 2; i < n; i++) {``            ``a2 = a2 * multiplyer + parseInt(s[i]);``        ``}``        ``let i = 1;``        ``if` `(a1 % a == 0 && a2 % b == 0) {``            ``let k1 = ``'1'``.repeat(s);``            ``let k2 = ``""``;``            ``for` `(let j = 1; j < n; j++)``                ``k2 += s[j];``            ``return` `k1 + ``" "` `+ k2; ``// return the numbers formed as string``        ``}``      ``// from here by using sliding window technique we will iterate and check for every i``      ``// that if the two current numbers formed are divisible if yes return``      ``// else form the two new numbers for next iteration using sliding window technique``        ``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"``;``}``// Driver code``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);``}`  `// This code is contributed by phasing17`

Output

```YES
12 3```

My Personal Notes arrow_drop_up