Given a big positive number N. The task is divide N into two number ‘A’ and ‘B’ such that difference between them is K (1 <= K <= 10100) i.e A – B = K.
Input : N = 10, K = 2 Output : A = 6 B = 4 Input : N = 20, K = 4 Output : A = 12 B = 8
Let the two required number be ‘A’ and ‘B’. So, we know sum of ‘A’ and ‘B’ will end upto N.
So, one equation became,
A + B = N
And also, we want the difference between ‘A’ and ‘B’ equals to ‘K’.
So, another equation becomes,
A – B = K
On adding both the equation, we get
2*A = N + K
So, A = (N + K)/2
Then we can find B by, B = (N – A)
Now, to handle the Big Integer, we have store Integers in character array and define a function to perform the operation on them.
Below is C implementation of this approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Divide a number into two parts
- Divide number into two parts divisible by given numbers
- Divide a number into two parts such that sum of digits is maximum
- Count number of ways to divide a number in 4 parts
- Find the number of ways to divide number into four parts such that a = c and b = d
- Divide N into K unique parts such that gcd of those parts is maximum
- Divide N into K parts in the form (X, 2X, ... , KX) for some value of X
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Minimum cuts required to divide the Circle into equal parts
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Split a number into 3 parts such that none of the parts is divisible by 3
- Break the number into three parts
- Partition a number into two divisible parts
- Possible cuts of a number such that maximum parts are divisible by 3
- Sum of all second largest divisors after splitting a number into one or more parts
- Find count of digits in a number that divide the number
- Number of ways N can be divided into four parts to construct a rectangle
- Break a number such that sum of maximum divisors of all parts is minimum
- Sum and Product of digits in a number that divide the number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.