- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

#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; }

YES 12 3

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.

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.

- Related Questions & Answers
- Divide number into two parts divisible by given numbers in C++ Program
- Divide a number into two parts in C++ Program
- Partition Array Into Three Parts With Equal Sum in Python
- Divide a big number into two parts that differ by k in C++ Program
- Count number of ways to partition a set into k subsets in C++
- Possible cuts of a number such that maximum parts are divisible by 3 in C++
- Partition Array into Disjoint Intervals in C++
- Break Number Into 3 parts to find max sum
- Splitting a string into parts in JavaScript
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Divide a string into n equal parts - JavaScript
- Splitting a string into maximum parts in JavaScript
- Splitting number into n parts close to each other in JavaScript
- Split string into equal parts JavaScript
- Check if a line at 45 degree can divide the plane into two equal weight parts in C++

Advertisements