# Reduce number to a single digit by subtracting adjacent digits repeatedly

Given a number N, the task is to reduce it to a single-digit number by repeatedly subtracting the adjacent digits. That is, in the first iteration, subtract all of the adjacent digits to generate a new number, if this number contains more than one digit, repeat the same process until it becomes a single-digit number.

Examples:

Input: N = 6972
Output: 2
| 6 – 9 | = 3
| 9 – 7 | = 2
| 7 – 2 | = 5

After first step we get 325 but 325 is not a single-digit number so we’ll further reduce it until we do not get single digit number.

| 3 – 2 | = 1
| 2 – 5 | = 3

And now the number will become 13, we’ll reduce it furthur
| 1 – 3 | = 2

Input: N = 123456
Output: 0

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

Approach: Here we are using Array to represent the inital number N for simplicity.

1. Count the number of digits in N and store the value in l.
2. Create an array a[] of size l.
3. Copy the given number into the array a[].
4. Calculate the RSF by subtracting the consecutive digits of array a.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the resultant digit ` `// after performing the given operations ` `int` `RSF(``int` `n) ` `{ ` `    ``while` `(n >= 10) { ` ` `  `        ``// Creating an extra copy of n ` `        ``int` `x = n; ` `        ``int` `l = 0; ` ` `  `        ``// Counting the number of digits in n ` `        ``while` `(n > 0) { ` `            ``n = n / 10; ` `            ``l++; ` `        ``} ` ` `  `        ``// Now n is 0 ` `        ``// Creating an array of length l ` `        ``int` `a[l]; ` ` `  `        ``// Initializing i with the last index of array ` `        ``int` `i = l - 1; ` `        ``while` `(x > 0) { ` ` `  `            ``// Filling array from right to left ` `            ``a[i] = x % 10; ` `            ``x = x / 10; ` `            ``i--; ` `        ``} ` ` `  `        ``// Calculating the absolute consecutive difference ` `        ``for` `(``int` `j = 0; j < l - 1; j++) { ` ` `  `            ``// Updating the value of n in every loop ` `            ``n = n * 10 + ``abs``(a[j] - a[j + 1]); ` `        ``} ` `    ``} ` ` `  `    ``// While loop ends here and we have found ` `    ``// the RSF value of N ` `    ``return` `n; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 614; ` ` `  `    ``// Passing n to RSF function and getting answer ` `    ``int` `ans = RSF(n); ` ` `  `    ``// Printing the value stored in ans ` `    ``cout << ans; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the resultant digit ` `    ``// after performing the given operations ` `    ``static` `int` `RSF(``int` `n) ` `    ``{ ` `        ``while` `(n >= ``10``) { ` ` `  `            ``// Creating an extra copy of n ` `            ``int` `x = n; ` `            ``int` `l = ``0``; ` ` `  `            ``// Counting the number of digits in n ` `            ``while` `(n > ``0``) { ` `                ``n = n / ``10``; ` `                ``l++; ` `            ``} ` ` `  `            ``// Now n is 0 ` `            ``// Creating an array of length l ` `            ``int` `a[] = ``new` `int``[l]; ` ` `  `            ``// Initializing i with the last index of array ` `            ``int` `i = l - ``1``; ` ` `  `            ``while` `(x > ``0``) { ` ` `  `                ``// Filling array from right to left ` `                ``a[i] = x % ``10``; ` `                ``x = x / ``10``; ` `                ``i--; ` `            ``} ` ` `  `            ``// Calculating the absolute consecutive difference ` `            ``for` `(``int` `j = ``0``; j < l - ``1``; j++) { ` ` `  `                ``// Updating the value of n in every loop ` `                ``n = n * ``10` `+ Math.abs(a[j] - a[j + ``1``]); ` `            ``} ` `        ``} ` ` `  `        ``// While loop ends here and we have found ` `        ``// the RSF value of N ` `        ``return` `n; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] arg) ` `    ``{ ` `        ``int` `n = ``6972``; ` ` `  `        ``// Passing n to RSF function and getting answer ` `        ``int` `ans = RSF(n); ` ` `  `        ``// Printing the value stored in ans ` `        ``System.out.println(ans); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the resultant digit ` `# after performing the given operations ` `def` `RSF(n): ` `    ``while` `(n >``=` `10``): ` ` `  `        ``# Creating an extra copy of n ` `        ``x ``=` `n; ` `        ``l ``=` `0``; ` ` `  `        ``# Counting the number of digits in n ` `        ``while` `(n > ``0``): ` `            ``n ``=` `n ``/``/` `10``; ` `            ``l ``+``=` `1``; ` ` `  `        ``# Now n is 0 ` `        ``# Creating an array of length l ` `        ``a ``=` `[``0``] ``*` `l; ` ` `  `        ``# Initializing i with the last index of array ` `        ``i ``=` `l ``-` `1``; ` `        ``while` `(x > ``0``): ` ` `  `            ``# Filling array from right to left ` `            ``a[i] ``=` `x ``%` `10``; ` `            ``x ``=` `x ``/``/` `10``; ` `            ``i ``-``=` `1``; ` ` `  `        ``# Calculating the absolute  ` `        ``# consecutive difference ` `        ``for` `j ``in` `range``(``0``, l ``-` `1``): ` ` `  `            ``# Updating the value of n in every loop ` `            ``n ``=` `n ``*` `10` `+` `abs``(a[j] ``-` `a[j ``+` `1``]); ` `             `  `    ``# While loop ends here and we have found ` `    ``# the RSF value of N ` `    ``return` `n; ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `614``; ` ` `  `    ``# Passing n to RSF function  ` `    ``# and getting answer ` `    ``ans ``=` `RSF(n); ` ` `  `    ``# Printing the value stored in ans ` `    ``print``(ans); ` ` `  `# This code is contributed by Rajput-Ji `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG  ` `{ ` ` `  `// Function to return the resultant digit ` `// after performing the given operations ` `static` `int` `RSF(``int` `n) ` `{ ` `    ``while` `(n >= 10)  ` `    ``{ ` ` `  `        ``// Creating an extra copy of n ` `        ``int` `x = n; ` `        ``int` `l = 0; ` ` `  `        ``// Counting the number of digits in n ` `        ``while` `(n > 0)  ` `        ``{ ` `            ``n = n / 10; ` `            ``l++; ` `        ``} ` ` `  `        ``// Now n is 0 ` `        ``// Creating an array of length l ` `        ``int` `[]a = ``new` `int``[l]; ` ` `  `        ``// Initializing i with the last index of array ` `        ``int` `i = l - 1; ` ` `  `        ``while` `(x > 0)  ` `        ``{ ` ` `  `            ``// Filling array from right to left ` `            ``a[i] = x % 10; ` `            ``x = x / 10; ` `            ``i--; ` `        ``} ` ` `  `        ``// Calculating the absolute ` `        ``// consecutive difference ` `        ``for` `(``int` `j = 0; j < l - 1; j++) ` `        ``{ ` ` `  `            ``// Updating the value of n in every loop ` `            ``n = n * 10 + Math.Abs(a[j] - a[j + 1]); ` `        ``} ` `    ``} ` ` `  `    ``// While loop ends here and we have found ` `    ``// the RSF value of N ` `    ``return` `n; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] arg) ` `{ ` `    ``int` `n = 6972; ` ` `  `    ``// Passing n to RSF function and getting answer ` `    ``int` `ans = RSF(n); ` ` `  `    ``// Printing the value stored in ans ` `    ``Console.WriteLine(ans); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```2
```

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.

My Personal Notes arrow_drop_up 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.

Improved By : princi singh, Rajput-Ji

Article Tags :
Practice Tags :

6

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