Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmNth term where K+1th term is product of Kth term with difference...

Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term

Given two integers N and D, the task is to find the value of F(N) where the value of F(1) is D, where F(K) is given as: 
 

F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))

Examples: 
 

Input: N = 3, D = 487 
Output: 15584 
Explanation: 
As F(1) = 487, 
F(2) = 487 * (maxDigit(487) – minDigit(487)) = 487 * 4 = 1948 
F(3) = 1948 * (maxDigit(1948) – minDigit(1948)) = 1948 * 8 = 15584
Input: N = 5, D = 487 
Output: 981792 
 

 

Approach: The idea is to compute the value of F(2) to F(N) iteratively with the help of the loop. Also, the maximum and minimum digits in each number can be computed using the help of the loop by dividing the number by 10, Simultaneously taking the modulo to get the digit.
Below is the implementation of the above approach:
 

C++




// C++ implementation to find the value
// of the given function for the value
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum digit
// in the decimal representation of N
int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n) {
        ans = min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n) {
        ans = max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k--) {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    cout << n;
}
 
// Driver Code
int main()
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
 
    return 0;
}


Java




// Java implementation to find the value
// of the given function for the value
 
class GFG{
 
// Function to find minimum digit in
// the decimal representation of N
static int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n > 0)
    {
        ans = Math.min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
static int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n > 0)
    {
        ans = Math.max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
static void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k-- > 0)
    {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    System.out.print(n);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation to find the value
# of the given function for the value
 
# Function to find minimum digit
# in the decimal representation of N
def MIN(n):
     
    ans = 11
     
    # Loop to find the minimum
    # digit in the number
    while n:
        ans = min(ans, n % 10)
        n //= 10
    return ans
 
# Function to find maximum digit in
# the decimal representation of N
def MAX(n):
     
    ans = -1
     
    # Loop to find the maximum
    # digit in the number
    while n:
        ans = max(ans, n % 10)
        n //= 10
    return ans
 
# Function to find the value
# of the given function
def Find_value(n, k):
     
    k -= 1
    (x, y) = (0, 0)
     
    # Loop to compute the values
    # of the given number
    while k:
        k -= 1
        x = MIN(n)
        y = MAX(n)
        if ((y - x) == 0):
            break
        n *= (y - x)
         
    print(n, end = ' ')
 
# Driver Code
if __name__=='__main__':
     
    (N, D) = (487, 5)
     
    # Function Call
    Find_value(N, D)
 
# This code is contributed by rutvik_56


C#




// C# implementation to find the value
// of the given function for the value
using System;
 
class GFG{
 
// Function to find minimum digit in
// the decimal representation of N
static int MIN(int n)
{
    int ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n > 0)
    {
        ans = Math.Min(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
static int MAX(int n)
{
    int ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n > 0)
    {
        ans = Math.Max(ans, n % 10);
        n /= 10;
    }
    return ans;
}
 
// Function to find the value
// of the given function
static void Find_value(int n, int k)
{
    k--;
    int x = 0;
    int y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k-- > 0)
    {
        x = MIN(n);
        y = MAX(n);
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    Console.Write(n);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// javascript implementation to find the value
// of the given function for the value
 
// Function to find minimum digit
// in the decimal representation of N
function MIN( n)
{
    let ans = 11;
 
    // Loop to find the minimum
    // digit in the number
    while (n) {
        ans = parseInt(Math.min(ans, n % 10));
        n = parseInt(n/ 10);
    }
    return ans;
}
 
// Function to find maximum digit
// in the decimal representation of N
function MAX( n)
{
    let ans = -1;
 
    // Loop to find the maximum
    // digit in the number
    while (n) {
        ans = parseInt(Math.max(ans, n % 10));
       n = parseInt(n/ 10);
    }
    return ans;
}
 
// Function to find the value
// of the given function
function Find_value( n,  k)
{
    k--;
    let x = 0;
    let y = 0;
 
    // Loop to compute the values
    // of the given number
    while (k--) {
        x = parseInt(MIN(n));
        y = parseInt(MAX(n));
 
        if (y - x == 0)
            break;
        n *= (y - x);
    }
    document.write(n);
}
 
// Driver Code
let N = 487, D = 5;
 
    // Function Call
    Find_value(N, D);
 
// This code contributed by gauravrajput1
 
</script>


Output: 

981792

 

Time Complexity: O(D*log(N))

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments