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.,
AN+1 = AN + max(digits of AN) * min(digits of AN)
Input: K = 1, N = 5
A1 = 1
A2 = A1 + minDigit( A1 ) * maxDigit( A1 ) = 1 + min(1) * max(1) = 1 + 1*1 = 2
A3 = A2 + minDigit( A2 ) * maxDigit( A2 ) = 2 + min(2) * max(2) = 2 + 2*2 = 6
A4 = A3 + minDigit( A3 ) * maxDigit( A3 ) = 6 + min(6) * max(6) = 6 + 6*6 = 42
A5 = A4 + minDigit( A4 ) * maxDigit( A4 ) = 42 + min(4, 2) * max(4, 2) = 42 + 2*4 = 50
Input: K = 487, N = 2
A1 = 487
A2 = A1 + minDigit( A1 ) * maxDigit( a1 ) = 487 + min(4, 8, 7) * max(4, 8, 7) = 487 + 4*8 = 519
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.
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Count sequences of length K having each term divisible by its preceding term
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Finding nth term of any Polynomial Sequence
- Program to find Nth term of the Van Eck's Sequence
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Find nth term of the Dragon Curve Sequence
- Print the sequence of size N in which every term is sum of previous K terms
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Digit - Product - Sequence
- Number formed by adding product of its max and min digit K times
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Product of N with its largest odd digit
- Smallest and Largest sum of two n-digit numbers
- Largest and smallest digit of a number
- Largest and Smallest N-digit Hexadecimal Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.