Number formed by adding product of its max and min digit K times
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 implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int prod_of_max_min( int n)
{
int largest = 0;
int smallest = 10;
while (n) {
int r = n % 10;
largest = max(r, largest);
smallest = min(r, smallest);
n = n / 10;
}
return largest * smallest;
}
int formed_no( int N, int K)
{
if (K == 1) {
return N;
}
K--;
int answer = N;
while (K--) {
int a_current
= prod_of_max_min(answer);
if (a_current == 0)
break ;
answer += a_current;
}
return answer;
}
int main()
{
int N = 487, K = 100000000;
cout << formed_no(N, K) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int formed_no( int N, int K)
{
if (K == 1 )
{
return N;
}
K--;
int answer = N;
while (K != 0 )
{
int a_current = prod_of_max_min(answer);
if (a_current == 0 )
break ;
answer += a_current;
}
return answer;
}
static int prod_of_max_min( int n)
{
int largest = 0 ;
int smallest = 10 ;
while (n != 0 )
{
int r = n % 10 ;
largest = Math.max(r, largest);
smallest = Math.min(r, smallest);
n = n / 10 ;
}
return largest * smallest;
}
public static void main(String[] args)
{
int N = 487 , K = 100000000 ;
System.out.println(formed_no(N, K));
}
}
|
Python3
def formed_no(N, K):
if (K = = 1 ):
return N
K - = 1
answer = N
while (K ! = 0 ):
a_current = prod_of_max_min(answer)
if (a_current = = 0 ):
break
answer + = a_current
K - = 1
return answer
def prod_of_max_min(n):
largest = 0
smallest = 10
while (n ! = 0 ):
r = n % 10
largest = max (r, largest)
smallest = min (r, smallest)
n = n / / 10
return largest * smallest
if __name__ = = "__main__" :
N = 487
K = 100000000
print (formed_no(N, K))
|
C#
using System;
class GFG {
public static int formed_no( int N, int K)
{
if (K == 1)
{
return N;
}
K--;
int answer = N;
while (K != 0)
{
int a_current = prod_of_max_min(answer);
if (a_current == 0)
break ;
answer += a_current;
}
return answer;
}
static int prod_of_max_min( int n)
{
int largest = 0;
int smallest = 10;
while (n != 0)
{
int r = n % 10;
largest = Math.Max(r, largest);
smallest = Math.Min(r, smallest);
n = n / 10;
}
return largest * smallest;
}
public static void Main(String[] args)
{
int N = 487, K = 100000000;
Console.WriteLine(formed_no(N, K));
}
}
|
Javascript
<script>
function prod_of_max_min(n)
{
var largest = 0;
var smallest = 10;
while (n)
{
var r = n % 10;
largest = Math.max(r, largest);
smallest = Math.min(r, smallest);
n = parseInt(n / 10);
}
return largest * smallest;
}
function formed_no(N, K)
{
if (K == 1)
{
return N;
}
K--;
var answer = N;
while (K--)
{
var a_current = prod_of_max_min(answer);
if (a_current == 0) break ;
answer += a_current;
}
return answer;
}
var N = 487,
K = 100000000;
document.write(formed_no(N, K) + "<br>" );
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...