Transform N to Minimum possible value
Last Updated :
08 May, 2023
Given two numbers and N and D. Apply any of two below operations to N:
- add D to N
- change N to digitsum(N), where digitsum(N) is the sum of digits of N
The task is to transform N to the minimum possible value. Print the minimum possible value of N and the number of times the given operations applied(any one of them). The number of operations must be minimum.
Examples:
Input : N = 2, D = 1
Output : 1 9
Perform Type1 operation 8 times and Type2 operation 1 time
Input : N = 9, D = 3
Output : 3, 2
Apply one type1 operation first and then type2 operation
Prerequisites:
1. Digital Root (repeated digital sum) of the given large integer
2. Numbers in a Range with given Digital Root
Approach :
Let Dr(x) be a function defined for integer x as :
- Dr(x) = x, if 0 <= x <= 9
- else, Dr(x) = Dr(Sum-of-digits(x))
The function Dr(x) is the digital root of a number x.
- Dr(a+b) = Dr(Dr(a) + Dr(b))
- Dr(ab) = Dr(Dr(a) * Dr(b))
Important observation : The minimum value is always the minimum over : Dr(N + kD) for some non-negative integer k.
Dr(N + kD) = Dr(Dr(N) + Dr(kD)) (1)
Now, Dr(kd) = Dr(Dr(k) * Dr(D))
Possible values of Dr(k) are 0, 1, 2…9, given by numbers k=0, 1, 2…9
Dr(x) = Dr(Sum-of-digits(x)) (2)
- The minimum value for N is equal to the minimum value for Sum-of-digits(N). If we reduce this answer once and add D, the minimum value that can be obtained wouldn’t change. So, if it is required to perform a reduce operation and then an add operation, then we can do the add operation and then the reduce operation without affecting the possible roots we can reach. This is evident from combination of formulae (1) and (2)
- So, we can do all add operations first, all reduce operations later, and reach any number that can be possibly reached by any set of operations. Using the above claims, we can prove the minimum possible value is the minimum of Dr(N + kD) where 0 <= k <= 9.
- To find the minimum number of steps, note that the relative order of the add and Sum-of-digits operations does affect the answer. Also, note that the Sum-of-digits function is an decreases extremely fast.
- Any number <= 1010 goes to a number <= 90, any number <= 90 goes to something <= 18 and so on. In short, any number can be reduced to its digital root in <= 5 steps.
- Via this, we can prove that the value of the minimum steps can never be greater than 15. This is a loose upper bound, not the exact one.
- Use brute force recursion algorithm, that at each step branches in 2 different directions, one x = Sum-of-digits(x), the other being x = x+D, but only until a recursion depth of 15. In this way, we stop after exploring 215 different ways.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_val = INT_MAX;
int min_steps = 0;
int sumOfDigits( int n)
{
string s = to_string(n);
int sum = 0;
for ( int i = 0; i < s.length(); i++) {
sum += (s[i] - '0' );
}
return sum;
}
void Transform( int n, int d, int steps)
{
if (n < min_val) {
min_val = n;
min_steps = steps;
}
else if (n == min_val) {
min_steps = min(min_steps, steps);
}
if (steps < 15) {
Transform(sumOfDigits(n), d, steps + 1);
Transform(n + d, d, steps + 1);
}
}
int main()
{
int N = 9, D = 3;
Transform(N, D, 0);
cout << min_val << " " << min_steps;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int min_val = Integer.MAX_VALUE;
static int min_steps = 0 ;
static int sumOfDigits( int n)
{
String s = String.valueOf(n);
int sum = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
sum += (s.charAt(i) - '0' );
}
return sum;
}
static void Transform( int n, int d, int steps)
{
if (n < min_val) {
min_val = n;
min_steps = steps;
}
else if (n == min_val) {
min_steps = Math.min(min_steps, steps);
}
if (steps < 15 ) {
Transform(sumOfDigits(n), d, steps + 1 );
Transform(n + d, d, steps + 1 );
}
}
public static void main(String[] args)
{
int N = 9 , D = 3 ;
Transform(N, D, 0 );
System.out.print(min_val+ " " + min_steps);
}
}
|
Python3
import sys;
min_val = sys.maxsize;
min_steps = 0 ;
def sumOfDigits(n) :
s = str (n);
sum = 0 ;
for i in range ( len (s)) :
sum + = ( ord (s[i]) - ord ( '0' ));
return sum ;
def Transform(n, d, steps) :
global min_val; global min_steps;
if (n < min_val) :
min_val = n;
min_steps = steps;
elif (n = = min_val) :
min_steps = min (min_steps, steps);
if (steps < 15 ) :
Transform(sumOfDigits(n), d, steps + 1 );
Transform(n + d, d, steps + 1 );
if __name__ = = "__main__" :
N = 9 ; D = 3 ;
Transform(N, D, 0 );
print (min_val, min_steps);
|
C#
using System;
class GFG{
static int min_val = int .MaxValue;
static int min_steps = 0;
static int sumOfDigits( int n)
{
string s = n.ToString();
int sum = 0;
for ( int i = 0; i < s.Length; i++) {
sum += (s[i] - '0' );
}
return sum;
}
static void Transform( int n, int d, int steps)
{
if (n < min_val) {
min_val = n;
min_steps = steps;
}
else if (n == min_val) {
min_steps = Math.Min(min_steps, steps);
}
if (steps < 15) {
Transform(sumOfDigits(n), d, steps + 1);
Transform(n + d, d, steps + 1);
}
}
public static void Main( string [] args)
{
int N = 9, D = 3;
Transform(N, D, 0);
Console.Write(min_val+ " " + min_steps);
}
}
|
Javascript
<script>
let min_val = Number.MAX_VALUE;
let min_steps = 0;
function sumOfDigits(n)
{
let s = n.toString();
let sum = 0;
for (let i = 0; i < s.length; i++) {
sum += (s[i] - '0' );
}
return sum;
}
function Transform(n, d, steps)
{
if (n < min_val) {
min_val = n;
min_steps = steps;
}
else if (n == min_val) {
min_steps = Math.min(min_steps, steps);
}
if (steps < 15) {
Transform(sumOfDigits(n), d, steps + 1);
Transform(n + d, d, steps + 1);
}
}
let N = 9, D = 3;
Transform(N, D, 0);
document.write(min_val+ " " + min_steps);
</script>
|
Time Complexity :
Space Complexity: O(15 + log10N)
Share your thoughts in the comments
Please Login to comment...