Reduce number to a single digit by subtracting adjacent digits repeatedly
Last Updated :
16 Dec, 2022
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 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 further
| 1 – 3 | = 2
Input: N = 123456
Output: 0
Approach: Here we are using Array to represent the initial 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:
C++
#include <bits/stdc++.h>
using namespace std;
int RSF( int n)
{
while (n >= 10) {
int x = n;
int l = 0;
while (n > 0) {
n = n / 10;
l++;
}
int a[l];
int i = l - 1;
while (x > 0) {
a[i] = x % 10;
x = x / 10;
i--;
}
for ( int j = 0; j < l - 1; j++) {
n = n * 10 + abs (a[j] - a[j + 1]);
}
}
return n;
}
int main()
{
int n = 614;
int ans = RSF(n);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int RSF( int n)
{
while (n >= 10 ) {
int x = n;
int l = 0 ;
while (n > 0 ) {
n = n / 10 ;
l++;
}
int a[] = new int [l];
int i = l - 1 ;
while (x > 0 ) {
a[i] = x % 10 ;
x = x / 10 ;
i--;
}
for ( int j = 0 ; j < l - 1 ; j++) {
n = n * 10 + Math.abs(a[j] - a[j + 1 ]);
}
}
return n;
}
public static void main(String[] arg)
{
int n = 6972 ;
int ans = RSF(n);
System.out.println(ans);
}
}
|
Python3
def RSF(n):
while (n > = 10 ):
x = n;
l = 0 ;
while (n > 0 ):
n = n / / 10 ;
l + = 1 ;
a = [ 0 ] * l;
i = l - 1 ;
while (x > 0 ):
a[i] = x % 10 ;
x = x / / 10 ;
i - = 1 ;
for j in range ( 0 , l - 1 ):
n = n * 10 + abs (a[j] - a[j + 1 ]);
return n;
if __name__ = = '__main__' :
n = 614 ;
ans = RSF(n);
print (ans);
|
C#
using System;
class GFG
{
static int RSF( int n)
{
while (n >= 10)
{
int x = n;
int l = 0;
while (n > 0)
{
n = n / 10;
l++;
}
int []a = new int [l];
int i = l - 1;
while (x > 0)
{
a[i] = x % 10;
x = x / 10;
i--;
}
for ( int j = 0; j < l - 1; j++)
{
n = n * 10 + Math.Abs(a[j] - a[j + 1]);
}
}
return n;
}
public static void Main(String[] arg)
{
int n = 6972;
int ans = RSF(n);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function RSF(n) {
while (n >= 10) {
var x = n;
var l = 0;
while (n > 0) {
n = parseInt(n / 10);
l++;
}
var a = Array(l).fill(0);
var i = l - 1;
while (x > 0) {
a[i] = x % 10;
x = parseInt(x / 10);
i--;
}
for (j = 0; j < l - 1; j++) {
n = n * 10 + Math.abs(a[j] - a[j + 1]);
}
}
return n;
}
var n = 6972;
var ans = RSF(n);
document.write(ans);
</script>
|
Time Complexity : O(n logn)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...