# 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 <= 10^{100}) 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

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 <stdio.h> ` `#include <string.h> ` `#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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

60 40

## Recommended Posts:

- 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
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Minimum cuts required to divide the Circle into equal parts
- 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 divisble 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
- Break a number such that sum of maximum divisors of all parts is minimum
- Find count of digits in a number that divide the number
- Split the number into N parts such that difference between the smallest and the largest part is minimum

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.