C++ Partition a Number into Two Divisible Parts


In this problem, we are given a string that can be interpreted as a number. Now we have to perform the partition that string into two parts such that the first part is divisible by A and the second part is divisible by B (two integers given to us). For example −

Input : str = "123", a = 12, b = 3
Output : YES
12 3
"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

Now in this problem, we will do some precalculation that will make our program faster, and then it will be able to work on higher constraints.

Approach to Find the Solution

In this approach, we will run two loops through the string, first from start to end and the second one from end to start. Now at every point, we take the mod of the integer formed with an in the first loop and with b in the second loop, and then we can find our answer.

Example

#include <bits/stdc++.h>
using namespace std;
void divisionOfString(string &str, int a, int b){
    int n = str.length();
    vector<int> mod_a(n+1, 0); //
    mod_a[0] = (str[0] - '0')%a;
    for (int i=1; i<n; i++) // front loop for calculating the mod of integer with a
        mod_a[i] = ((mod_a[i-1]*10)%a + (str[i]-'0'))%a;
    vector<int> mod_b(n+1, 0);
    mod_b[n-1] = (str[n-1] - '0')%b;
    int power10 = 10; // as we have assigned answer to last index
    for (int i= n-2; i>=0; i--){ // end loop for calculating the mod of integer with b
        mod_b[i] = (mod_b[i+1] + (str[i]-'0')*power10)%b;
        power10 = (power10 * 10) % b;
    }
    for (int i=0; i<n-1; i++){ // finding the division point
        if (mod_a[i] != 0) // we can skip through all the positions where mod_a is not zero
            continue;
        if (mod_b[i+1] == 0){ // now if the next index of mod_b is also zero so that is our division point
            cout << "YES\n";
            /*******Printing the partitions formed**********/
            for (int k=0; k<=i; k++)
               cout << str[k];
            cout << " ";
            for (int k=i+1; k < n; k++)
               cout << str[k];
            return;
        }
    }
    cout << "NO\n"; // else we print NO
}
// Driver code
int main(){
    string str = "123"; // given string
    int a = 12, b = 3;
    divisionOfString(str, a, b);
    return 0;
}

Output

YES
12 3

Explanation of the Above Code

In this approach, we calculated the remainder of the number formed at every division now. Our first number should be divisible by a, so we run a forward loop and store the mod of that number with a. With b, we run a backward loop and store the mods now as we know that if our mod of an at any position is zero and mod with b with the next index is zero, that will be our answer, and hence we print it.

Conclusion

In this tutorial, we solve a problem to find Partition a number into two divisible parts. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 25-Nov-2021

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements