Open In App

Transform N to Minimum possible value

Given two numbers and N and D. Apply any of two below operations to N
 

  1. add D to N
  2. 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 :
 


The function Dr(x) is the digital root of a number x. 
 


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)


Below is the implementation of the above approach: 
 

// CPP program to transform N to the minimum value
#include <bits/stdc++.h>
using namespace std;
 
// Initialising the answer
int min_val = INT_MAX;
int min_steps = 0;
 
// Function to find the digitsum
int sumOfDigits(int n)
{
    string s = to_string(n);
 
    int sum = 0;
 
    // Iterate over all digits and add them
    for (int i = 0; i < s.length(); i++) {
        sum += (s[i] - '0');
    }
     
    // Return the digit su,
    return sum;
}
 
// Function to transform N to the minimum value
void Transform(int n, int d, int steps)
{
    // If the final value is lesser than least value
    if (n < min_val) {
        min_val = n;
        min_steps = steps;
    }
 
    // If final value is equal to least value then check
    // for lesser number of steps to reach this value
    else if (n == min_val) {
        min_steps = min(min_steps, steps);
    }
 
    // The value will be obtained in less than 15 steps as
    // proved so applying normal recursive operations
    if (steps < 15) {
        Transform(sumOfDigits(n), d, steps + 1);
        Transform(n + d, d, steps + 1);
    }
}
 
// Driver code
int main()
{
    int N = 9, D = 3;
     
    // Function call
    Transform(N, D, 0);
     
    // Print the answers
    cout << min_val << " " << min_steps;
     
    return 0;
}

                    
// JAVA program to transform N to the minimum value
import java.util.*;
 
class GFG{
  
// Initialising the answer
static int min_val = Integer.MAX_VALUE;
static int min_steps = 0;
  
// Function to find the digitsum
static int sumOfDigits(int n)
{
    String s = String.valueOf(n);
  
    int sum = 0;
  
    // Iterate over all digits and add them
    for (int i = 0; i < s.length(); i++) {
        sum += (s.charAt(i) - '0');
    }
      
    // Return the digit su,
    return sum;
}
  
// Function to transform N to the minimum value
static void Transform(int n, int d, int steps)
{
    // If the final value is lesser than least value
    if (n < min_val) {
        min_val = n;
        min_steps = steps;
    }
  
    // If final value is equal to least value then check
    // for lesser number of steps to reach this value
    else if (n == min_val) {
        min_steps = Math.min(min_steps, steps);
    }
  
    // The value will be obtained in less than 15 steps as
    // proved so applying normal recursive operations
    if (steps < 15) {
        Transform(sumOfDigits(n), d, steps + 1);
        Transform(n + d, d, steps + 1);
    }
}
  
// Driver code
public static void main(String[] args)
{
    int N = 9, D = 3;
      
    // Function call
    Transform(N, D, 0);
      
    // Print the answers
    System.out.print(min_val+ " " +  min_steps);
      
}
}
 
// This code is contributed by 29AjayKumar

                    
# Python3 program to transform N to the minimum value
import sys;
 
# Initialising the answer
min_val = sys.maxsize;
min_steps = 0;
 
# Function to find the digitsum
def sumOfDigits(n) :
 
    s = str(n);
 
    sum = 0;
 
    # Iterate over all digits and add them
    for i in range(len(s)) :
        sum += (ord(s[i]) - ord('0'));
     
    # Return the digit su,
    return sum;
 
# Function to transform N to the minimum value
def Transform(n, d, steps) :
    global min_val;global min_steps;
     
    # If the final value is lesser than least value
    if (n < min_val) :
        min_val = n;
        min_steps = steps;
 
    # If final value is equal to least value then check
    # for lesser number of steps to reach this value
    elif (n == min_val) :
        min_steps = min(min_steps, steps);
     
    # The value will be obtained in less than 15 steps as
    # proved so applying normal recursive operations
    if (steps < 15) :
        Transform(sumOfDigits(n), d, steps + 1);
        Transform(n + d, d, steps + 1);
 
# Driver code
if __name__ == "__main__" :
 
    N = 9; D = 3;
     
    # Function call
    Transform(N, D, 0);
     
    # Print the answers
    print(min_val, min_steps);
     
# This code is contributed by Yash_R

                    
// C# program to transform N to the minimum value
using System;
 
class GFG{
  
// Initialising the answer
static int min_val = int.MaxValue;
static int min_steps = 0;
  
// Function to find the digitsum
static int sumOfDigits(int n)
{
    string s = n.ToString();
  
    int sum = 0;
  
    // Iterate over all digits and add them
    for (int i = 0; i < s.Length; i++) {
        sum += (s[i] - '0');
    }
      
    // Return the digit su,
    return sum;
}
  
// Function to transform N to the minimum value
static void Transform(int n, int d, int steps)
{
    // If the final value is lesser than least value
    if (n < min_val) {
        min_val = n;
        min_steps = steps;
    }
  
    // If final value is equal to least value then check
    // for lesser number of steps to reach this value
    else if (n == min_val) {
        min_steps = Math.Min(min_steps, steps);
    }
  
    // The value will be obtained in less than 15 steps as
    // proved so applying normal recursive operations
    if (steps < 15) {
        Transform(sumOfDigits(n), d, steps + 1);
        Transform(n + d, d, steps + 1);
    }
}
  
// Driver code
public static void Main(string[] args)
{
    int N = 9, D = 3;
      
    // Function call
    Transform(N, D, 0);
      
    // Print the answers
    Console.Write(min_val+ " " +  min_steps);
}
}
 
// This code is contributed by Yash_R

                    
<script>
// Javascript program to transform N to the minimum value
 
// Let initialising the answer
let min_val = Number.MAX_VALUE;
let min_steps = 0;
  
// Function to find the digitsum
function sumOfDigits(n)
{
    let s = n.toString();
  
    let sum = 0;
  
    // Iterate over all digits and add them
    for (let i = 0; i < s.length; i++) {
        sum += (s[i] - '0');
    }
      
    // Return the digit su,
    return sum;
}
  
// Function to transform N to the minimum value
function Transform(n, d, steps)
{
    // If the final value is lesser than least value
    if (n < min_val) {
        min_val = n;
        min_steps = steps;
    }
  
    // If final value is equal to least value then check
    // for lesser number of steps to reach this value
    else if (n == min_val) {
        min_steps = Math.min(min_steps, steps);
    }
  
    // The value will be obtained in less than 15 steps as
    // proved so applying normal recursive operations
    if (steps < 15) {
        Transform(sumOfDigits(n), d, steps + 1);
        Transform(n + d, d, steps + 1);
    }
}
 
// Driver Code
     
    let N = 9, D = 3;
      
    // Function call
    Transform(N, D, 0);
      
    // Print the answers
    document.write(min_val+ " " +  min_steps);
     
</script>

                    

Output: 
3 2

 

Time Complexity : 

Space Complexity: O(15 + log10N)
 


Article Tags :