Given two integers **N** and **K**, the task is to print the number formed by adding product of its max and min digit, K times.

**Examples**

Input:N = 14, K = 3

Output:26

Explanation:

M(0)=14

M(1)=14 + 1*4 = 18

M(2)=18 + 1*8 = 26

Input:N = 487, K = 100000000

Output: 950

**Approach**

- A natural intuition is to run a loop K times and keep updating the value of N.
- But one observation can be observed that after some iteration minimum value of digit may be zero and after that N is never going to be updated because:

M(N + 1) = M(N) + 0*(max_digit)

M(N + 1) = M(N) - Hence we just need to figure out when minimum digit became 0.

Below is the C++ implementation of the above approach:

## C++

`// C++ Code for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function that returns the product of ` `// maximum and minimum digit of N number. ` `int` `prod_of_max_min(` `int` `n) ` `{ ` ` ` `int` `largest = 0; ` ` ` `int` `smallest = 10; ` ` ` ` ` `while` `(n) { ` ` ` ` ` `// finds the last digit. ` ` ` `int` `r = n % 10; ` ` ` ` ` `largest = max(r, largest); ` ` ` `smallest = min(r, smallest); ` ` ` ` ` `// Moves to next digit ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `return` `largest * smallest; ` `} ` ` ` `// Function to find the formed number ` `int` `formed_no(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `if` `(K == 1) { ` ` ` `return` `N; ` ` ` `} ` ` ` `K--; ` `// M(1) = N ` ` ` ` ` `int` `answer = N; ` ` ` `while` `(K--) { ` ` ` ` ` `int` `a_current ` ` ` `= prod_of_max_min(answer); ` ` ` ` ` `// check if minimum digit is 0 ` ` ` `if` `(a_current == 0) ` ` ` `break` `; ` ` ` ` ` `answer += a_current; ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 487, K = 100000000; ` ` ` ` ` `cout << formed_no(N, K) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Function to find the formed number ` `public` `static` `int` `formed_no(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(K == ` `1` `) ` ` ` `{ ` ` ` `return` `N; ` ` ` `} ` ` ` `K--; ` `// M(1) = N ` ` ` ` ` `int` `answer = N; ` ` ` `while` `(K != ` `0` `) ` ` ` `{ ` ` ` `int` `a_current = prod_of_max_min(answer); ` ` ` ` ` `// Check if minimum digit is 0 ` ` ` `if` `(a_current == ` `0` `) ` ` ` `break` `; ` ` ` ` ` `answer += a_current; ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Function that returns the product of ` `// maximum and minimum digit of N number. ` `static` `int` `prod_of_max_min(` `int` `n) ` `{ ` ` ` `int` `largest = ` `0` `; ` ` ` `int` `smallest = ` `10` `; ` ` ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` ` ` `// Finds the last digit. ` ` ` `int` `r = n % ` `10` `; ` ` ` ` ` `largest = Math.max(r, largest); ` ` ` `smallest = Math.min(r, smallest); ` ` ` ` ` `// Moves to next digit ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` `return` `largest * smallest; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `487` `, K = ` `100000000` `; ` ` ` ` ` `System.out.println(formed_no(N, K)); ` `} ` `} ` ` ` `// This code is contributed by coder001 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find the formed number ` `def` `formed_no(N, K): ` ` ` ` ` `if` `(K ` `=` `=` `1` `): ` ` ` `return` `N ` ` ` `K ` `-` `=` `1` `# M(1) = N ` ` ` ` ` `answer ` `=` `N ` ` ` `while` `(K !` `=` `0` `): ` ` ` ` ` `a_current ` `=` `prod_of_max_min(answer) ` ` ` ` ` `# Check if minimum digit is 0 ` ` ` `if` `(a_current ` `=` `=` `0` `): ` ` ` `break` ` ` ` ` `answer ` `+` `=` `a_current ` ` ` `K ` `-` `=` `1` ` ` ` ` `return` `answer ` ` ` `# Function that returns the product of ` `# maximum and minimum digit of N number. ` `def` `prod_of_max_min(n): ` ` ` ` ` `largest ` `=` `0` ` ` `smallest ` `=` `10` ` ` ` ` `while` `(n !` `=` `0` `): ` ` ` ` ` `# Find the last digit. ` ` ` `r ` `=` `n ` `%` `10` ` ` ` ` `largest ` `=` `max` `(r, largest) ` ` ` `smallest ` `=` `min` `(r, smallest) ` ` ` ` ` `# Moves to next digit ` ` ` `n ` `=` `n ` `/` `/` `10` ` ` ` ` `return` `largest ` `*` `smallest ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `487` ` ` `K ` `=` `100000000` ` ` ` ` `print` `(formed_no(N, K)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to find the formed number ` `public` `static` `int` `formed_no(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(K == 1) ` ` ` `{ ` ` ` `return` `N; ` ` ` `} ` ` ` `K--; ` `// M(1) = N ` ` ` ` ` `int` `answer = N; ` ` ` `while` `(K != 0) ` ` ` `{ ` ` ` `int` `a_current = prod_of_max_min(answer); ` ` ` ` ` `// Check if minimum digit is 0 ` ` ` `if` `(a_current == 0) ` ` ` `break` `; ` ` ` ` ` `answer += a_current; ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Function that returns the product of ` `// maximum and minimum digit of N number. ` `static` `int` `prod_of_max_min(` `int` `n) ` `{ ` ` ` `int` `largest = 0; ` ` ` `int` `smallest = 10; ` ` ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` ` ` `// Finds the last digit. ` ` ` `int` `r = n % 10; ` ` ` ` ` `largest = Math.Max(r, largest); ` ` ` `smallest = Math.Min(r, smallest); ` ` ` ` ` `// Moves to next digit ` ` ` `n = n / 10; ` ` ` `} ` ` ` `return` `largest * smallest; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 487, K = 100000000; ` ` ` ` ` `Console.WriteLine(formed_no(N, K)); ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

**Output:**

950

**Time Complexity:** O(K)

**Auxiliary Space:** O(1)

## Recommended Posts:

- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Min-Max Product Tree of a given Binary Tree
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Check if the product of digit sum and its reverse equals the number or not
- Find the remainder when First digit of a number is divided by its Last digit
- Smallest N digit number with none of its digits as its divisor
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count of N-digit numbers having digit XOR as single digit
- Sum of width (max and min diff) of all Subsequences
- Partition N into M parts such that difference between Max and Min part is smallest
- Min and max length subarray having adjacent element difference atmost K
- Longest subarray such that difference of max and min is at-most K
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Product of N with its largest odd digit
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.

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.