# Divide a big number into two parts that differ by k

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.

Examples:

```Input : N = 10, K = 2
Output : A = 6 B = 4

Input : N = 20, K = 4
Output : A = 12 B = 8
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

 `// C program to Divide a Big ` `// Number into two parts ` `#include ` `#include ` `#define MAX 100 ` ` `  `// Function to adds two Numbers ` `// represnted as array of character. ` `void` `add(``char` `v1[], ``char` `v2[]) ` `{ ` `    ``int` `i, d, c = 0; ` ` `  `    ``// length of string ` `    ``int` `l1 = ``strlen``(v1); ` `    ``int` `l2 = ``strlen``(v2); ` ` `  `    ``// initializing extra character ` `    ``// position to 0 ` `    ``for` `(i = l1; i < l2; i++) ` `        ``v1[i] = ``'0'``; ` ` `  `    ``for` `(i = l2; i < l1; i++) ` `        ``v2[i] = ``'0'``; ` ` `  `    ``// Adding each element of character ` `    ``// and storing the carry. ` `    ``for` `(i = 0; i < l1 || i < l2; i++) { ` `        ``d = (v1[i] - ``'0'``) + (v2[i] - ``'0'``) + c; ` `        ``c = d / 10; ` `        ``d %= 10; ` `        ``v1[i] = ``'0'` `+ d; ` `    ``} ` ` `  `    ``// If remainder remains. ` `    ``while` `(c) { ` `        ``v1[i] = ``'0'` `+ (c % 10); ` `        ``c /= 10; ` `        ``i++; ` `    ``} ` ` `  `    ``v1[i] = ``'\0'``; ` `    ``v2[l2] = ``'\0'``; ` `} ` ` `  `// Function to subtracts two numbers ` `// represnted by string. ` `void` `subs(``char` `v1[], ``char` `v2[]) ` `{ ` `    ``int` `i, d, c = 0; ` ` `  `    ``// Finding the length of the string. ` `    ``int` `l1 = ``strlen``(v1); ` `    ``int` `l2 = ``strlen``(v2); ` ` `  `    ``// initializing extra character position to 0. ` `    ``for` `(i = l2; i < l1; i++) ` `        ``v2[i] = ``'0'``; ` ` `  `    ``// Substrating each element of character. ` `    ``for` `(i = 0; i < l1; i++) { ` `        ``d = (v1[i] - ``'0'` `- c) - (v2[i] - ``'0'``); ` ` `  `        ``if` `(d < 0) { ` `            ``d += 10; ` `            ``c = 1; ` `        ``} ` `        ``else` `            ``c = 0; ` ` `  `        ``v1[i] = ``'0'` `+ d; ` `    ``} ` ` `  `    ``v2[l2] = ``'\0'``; ` `    ``i = l1 - 1; ` ` `  `    ``while` `(i > 0 && v1[i] == ``'0'``) ` `        ``i--; ` ` `  `    ``v1[i + 1] = ``'\0'``; ` `} ` ` `  `// Function divides a number represented by ` `// character array a constant. ` `int` `divi(``char` `v[], ``int` `q) ` `{ ` `    ``int` `i, l = ``strlen``(v); ` ` `  `    ``int` `c = 0, d; ` ` `  `    ``// Dividing each character element by constant. ` `    ``for` `(i = l - 1; i >= 0; i--) { ` `        ``d = c * 10 + (v[i] - ``'0'``); ` `        ``c = d % q; ` `        ``d /= q; ` `        ``v[i] = ``'0'` `+ d; ` `    ``} ` ` `  `    ``i = l - 1; ` ` `  `    ``while` `(i > 0 && v[i] == ``'0'``) ` `        ``i--; ` ` `  `    ``v[i + 1] = ``'\0'``; ` `    ``return` `c; ` `} ` ` `  `// Function to reverses the character array. ` `void` `rev(``char` `v[]) ` `{ ` `    ``int` `l = ``strlen``(v); ` `    ``int` `i; ` `    ``char` `cc; ` ` `  `    ``// Reversing the array. ` `    ``for` `(i = 0; i < l - 1 - i; i++) { ` `        ``cc = v[i]; ` `        ``v[i] = v[l - 1 - i]; ` `        ``v[l - i - 1] = cc; ` `    ``} ` `} ` ` `  `// Wrapper Function ` `void` `divideWithDiffK(``char` `a[], ``char` `k[]) ` `{ ` ` `  `    ``// Reversing the character array. ` `    ``rev(a); ` `    ``rev(k); ` ` `  `    ``// Adding the each element of both array ` `    ``// and storing the sum in array a[]. ` `    ``add(a, k); ` ` `  `    ``// Dividing the array a[] by 2. ` `    ``divi(a, 2); ` ` `  `    ``// Reversing the character array to get output. ` `    ``rev(a); ` `    ``printf``(``"%s "``, a); ` ` `  `    ``// Substracting each element of array ` `    ``// i.e calculating a = a - b ` `    ``rev(a); ` `    ``subs(a, k); ` ` `  `    ``// Reversing the character array to get output. ` `    ``rev(a); ` `    ``printf``(``"%s"``, a); ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``char` `a[MAX] = ``"100"``, k[MAX] = ``"20"``; ` `    ``divideWithDiffK(a, k); ` `    ``return` `0; ` `} `

Output:

```60 40
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.