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.
Input: N = 6972
| 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
Approach: Here we are using Array to represent the inital number N for simplicity.
- Count the number of digits in N and store the value in l.
- Create an array a of size l.
- Copy the given number into the array a.
- Calculate the RSF by subtracting the consecutive digits of array a.
Below is the implementation of the above 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.
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Finding sum of digits of a number until sum becomes single digit
- Maximum of sum and product of digits until number is reduced to a single digit
- Sum of Digits in a^n till a single digit
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Reduce the number to minimum multiple of 4 after removing the digits
- Reduce the array to a single integer with the given operation
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Check if given number contains a digit which is the average of all other digits
- Find a N-digit number such that it is not divisible by any of its digits
- Smallest N digit number with none of its digits as its divisor
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Integers from the range that are composed of a single distinct digit
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Largest number in given Array formed by repeatedly combining two same elements
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subsequence such that adjacent elements have at least one common digit
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of 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 email@example.com. 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.