
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- 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++
- Break Number Into 3 parts to find max sum
- Possible cuts of a number such that maximum parts are divisible by 3 in C++
- Partition Array into Disjoint Intervals in C++
- A number is divided into two parts such that one part is six more than the other. If the two parts are in a ratio $6:5$, find the small part.
- Splitting number into n parts close to each other in JavaScript
- Divide 29 into two parts so that the sum of the squares of the parts is 425.
- Program to partition two strings such that each partition forms anagram in Python
- Splitting a string into parts in JavaScript
- How to divide an unknown integer into a given number of even parts using JavaScript?
- Divide a string into n equal parts - JavaScript
