Given two numbers **N** and **K**, where K represents the starting term of the sequence. The task is to find the **Nth term** of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e.,

A

_{N+1}= A_{N}+ max(digits of A_{N}) * min(digits of A_{N})

**Examples:**

Input:K = 1, N = 5

Output:50

Explanation:

A_{1}= 1

A_{2}= A_{1}+ minDigit( A_{1}) * maxDigit( A_{1}) = 1 + min(1) * max(1) = 1 + 1*1 = 2

A_{3}= A_{2}+ minDigit( A_{2}) * maxDigit( A_{2}) = 2 + min(2) * max(2) = 2 + 2*2 = 6

A_{4}= A_{3}+ minDigit( A_{3}) * maxDigit( A_{3}) = 6 + min(6) * max(6) = 6 + 6*6 = 42

A_{5}= A_{4}+ minDigit( A_{4}) * maxDigit( A_{4}) = 42 + min(4, 2) * max(4, 2) = 42 + 2*4 = 50

Input:K = 487, N = 2

Output:519

Explanation:

A_{1}= 487

A_{2}= A_{1}+ minDigit( A_{1}) * maxDigit( a_{1}) = 487 + min(4, 8, 7) * max(4, 8, 7) = 487 + 4*8 = 519

**Approach:**

Let us try to see some observations,

When K = 1, the sequence becomes: 1, 2, 6, 42, 50, 50, 50, …

When K = 2, the sequence becomes: 2, 6, 42, 50, 50, 50, …

.

.

When K = 5, the sequence becomes: 5, 30, 30, 30, 30, 30, …

.

.

Similarly, When K = 10, the sequence becomes: 10, 10, 10, 10, 10, 10, …

From the above examples, it can be observed that the sequence eventually stops increasing after an integer has at least one digit becomes 0. If any digit becomes 0, then the minimum digit would be always 0 and after that, all the integers in the sequence remain the same.

So the approach is to find the terms of the sequence till any 0 is encountered in the digits of the current term,

Below is the implementation of the above approach.

## C++

`// C++ program for the above approach. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find integer ` `int` `find(` `int` `K, ` `int` `N) ` `{ ` ` ` ` ` `// because 1st integer is K itself ` ` ` `N--; ` ` ` ` ` `while` `(N--) { ` ` ` `int` `curr_term = K; ` ` ` ` ` `// Initialize min_d and max_d ` ` ` `int` `min_d = 9; ` ` ` `int` `max_d = 0; ` ` ` ` ` `while` `(curr_term > 0) { ` ` ` `int` `r = curr_term % 10; ` ` ` ` ` `// updating min_d and max_d ` ` ` `min_d = min(min_d, r); ` ` ` `max_d = max(max_d, r); ` ` ` ` ` `curr_term = curr_term / 10; ` ` ` `} ` ` ` ` ` `// break if min digit is 0 ` ` ` `if` `(min_d == 0) { ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `K = K + min_d * max_d; ` ` ` `} ` ` ` ` ` `return` `K; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `K = 487; ` ` ` `int` `N = 2; ` ` ` ` ` `cout << find(K, N) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach. ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find integer ` `static` `int` `find(` `int` `K, ` `int` `N) ` `{ ` ` ` ` ` `// Because 1st integer is K itself ` ` ` `N--; ` ` ` ` ` `while` `(N-- != ` `0` `) ` ` ` `{ ` ` ` `int` `curr_term = K; ` ` ` ` ` `// Initialize min_d and max_d ` ` ` `int` `min_d = ` `9` `; ` ` ` `int` `max_d = ` `0` `; ` ` ` ` ` `while` `(curr_term > ` `0` `) ` ` ` `{ ` ` ` `int` `r = curr_term % ` `10` `; ` ` ` ` ` `// Updating min_d and max_d ` ` ` `min_d = Math.min(min_d, r); ` ` ` `max_d = Math.max(max_d, r); ` ` ` ` ` `curr_term = curr_term / ` `10` `; ` ` ` `} ` ` ` ` ` `// Break if min digit is 0 ` ` ` `if` `(min_d == ` `0` `) ` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `K = K + min_d * max_d; ` ` ` `} ` ` ` `return` `K; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `K = ` `487` `; ` ` ` `int` `N = ` `2` `; ` ` ` ` ` `System.out.print(find(K, N) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach. ` ` ` `# Function to find integer ` `def` `find(K, N): ` ` ` ` ` `# Because 1st integer is K itself ` ` ` `N ` `=` `N ` `-` `1` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, N): ` ` ` `curr_term ` `=` `K ` ` ` ` ` `# Initialize min_d and max_d ` ` ` `min_d ` `=` `9` ` ` `max_d ` `=` `0` ` ` ` ` `while` `curr_term > ` `0` `: ` ` ` `r ` `=` `int` `(curr_term ` `%` `10` `) ` ` ` ` ` `# Updating min_d and max_d ` ` ` `min_d ` `=` `min` `(min_d, r) ` ` ` `max_d ` `=` `max` `(max_d, r) ` ` ` ` ` `curr_term ` `=` `int` `(curr_term ` `/` `10` `) ` ` ` ` ` `# Break if min digit is 0 ` ` ` `if` `min_d ` `=` `=` `0` `: ` ` ` `break` ` ` ` ` `K ` `=` `K ` `+` `min_d ` `*` `max_d ` ` ` `return` `K ` ` ` `# Driver code ` `K ` `=` `487` `N ` `=` `2` ` ` `print` `(find(K, N)) ` ` ` `# This code is contributed by ishayadav181 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach. ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find integer ` `static` `int` `find(` `int` `K, ` `int` `N) ` `{ ` ` ` ` ` `// Because 1st integer is K itself ` ` ` `N--; ` ` ` ` ` `while` `(N-- != 0) ` ` ` `{ ` ` ` `int` `curr_term = K; ` ` ` ` ` `// Initialize min_d and max_d ` ` ` `int` `min_d = 9; ` ` ` `int` `max_d = 0; ` ` ` ` ` `while` `(curr_term > 0) ` ` ` `{ ` ` ` `int` `r = curr_term % 10; ` ` ` ` ` `// Updating min_d and max_d ` ` ` `min_d = Math.Min(min_d, r); ` ` ` `max_d = Math.Max(max_d, r); ` ` ` ` ` `curr_term = (` `int` `)(curr_term / 10); ` ` ` `} ` ` ` ` ` `// Break if min digit is 0 ` ` ` `if` `(min_d == 0) ` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `K = K + min_d * max_d; ` ` ` `} ` ` ` `return` `K; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `K = 487; ` ` ` `int` `N = 2; ` ` ` ` ` `Console.Write(find(K, N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

519

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Number formed by adding product of its max and min digit K times
- Smallest N digit number with none of its digits as its divisor
- Product of N with its largest odd digit
- Largest number less than N with digit sum greater than the digit sum of N
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Finding nth term of any Polynomial Sequence
- Find nth term of the Dragon Curve Sequence
- Program to find Nth term of the Van Eck's Sequence
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Check if the product of digit sum and its reverse equals the number or not
- Smallest and Largest sum of two n-digit numbers
- Length of smallest sequence having sum X and product Y
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Count sequences of length K having each term divisible by its preceding term

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.