Find an integer in the given range that satisfies the given conditions - GeeksforGeeks

14 Sep, 2021
Given two integers L and R where L ≤ R, the task is to find an integer K such that:  
L ≤ K ≤ R.All the digits of K are distinct.The value of the expression (L – K) * (K – R) is maximum.  
L ≤ K ≤ R.
All the digits of K are distinct.
The value of the expression (L – K) * (K – R) is maximum.  
If multiple answers exist then choose the larger value for K.Examples:  
Input: L = 5, R = 10 Output: 8Input: L = 50, R = 60 Output: 56  
 
Approach: Iterate from L to R and for each value of K, check whether it contains all distinct digits and (L – K) * (K – R) is maximum. If two or more values give the same maximum value for the expression then choose the greater value for K.Below is the implementation of the above approach:  
C++
Java
Python 3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; const int MAX = 10; // Function that returns true if x// contains all distinct digitsbool distinctDigits(int x){    bool present[MAX] = { false };     while (x > 0) {         // Last digit of x        int digit = x % 10;         // If current digit has        // appeared before        if (present[digit])            return false;         // Mark the current digit        // to present        present[digit] = true;         // Remove the last digit        x /= 10;    }     return true;} // Function to return the// required value of kint findK(int l, int r){     // To store the maximum value    // for the given expression    int maxExp = INT_MIN;    int k = -1;    for (int i = l; i <= r; i++) {         // If i contains all distinct digits        if (distinctDigits(i)) {            int exp = (l - i) * (i - r);             // If the value of the expression            // is also maximum then update k            // and the expression            if (exp >= maxExp) {                k = i;                maxExp = exp;            }        }    }     return k;} // Driver codeint main(){    int l = 50, r = 60;     cout << findK(l, r);     return 0;}
// Java implementation of the approachclass GFG{ static int MAX = 10; // Function that returns true if x// contains all distinct digitsstatic boolean distinctDigits(int x){    boolean []present = new boolean[MAX];     while (x > 0)    {         // Last digit of x        int digit = x % 10;         // If current digit has        // appeared before        if (present[digit])            return false;         // Mark the current digit        // to present        present[digit] = true;         // Remove the last digit        x /= 10;    }     return true;} // Function to return the// required value of kstatic int findK(int l, int r){     // To store the maximum value    // for the given expression    int maxExp = Integer.MIN_VALUE;    int k = -1;    for (int i = l; i <= r; i++)    {         // If i contains all distinct digits        if (distinctDigits(i))        {            int exp = (l - i) * (i - r);             // If the value of the expression            // is also maximum then update k            // and the expression            if (exp >= maxExp)            {                k = i;                maxExp = exp;            }        }    }     return k;} // Driver codepublic static void main(String[] args){    int l = 50, r = 60;     System.out.print(findK(l, r)); }} // This code is contributed by 29AjayKumar
# Python3 implementation of the approachimport sysMAX = 10 # Function that returns true if x# contains all distinct digitsdef distinctDigits(x):    present = [False for i in range(MAX)]     while (x > 0):                 # Last digit of x        digit = x % 10         # If current digit has        # appeared before        if (present[digit]):            return False         # Mark the current digit        # to present        present[digit] = True         # Remove the last digit        x = x // 10     return True # Function to return the# required value of kdef findK(l, r):         # To store the maximum value    # for the given expression    maxExp = -sys.maxsize - 1    k = -1    for i in range(l, r + 1, 1):                 # If i contains all distinct digits        if (distinctDigits(i)):            exp = (l - i) * (i - r)             # If the value of the expression            # is also maximum then update k            # and the expression            if (exp >= maxExp):                k = i;                maxExp = exp    return k # Driver codeif __name__ == '__main__':    l = 50    r = 60     print(findK(l, r))     # This code is contributed by Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{static int MAX = 10; // Function that returns true if x// contains all distinct digitsstatic bool distinctDigits(int x){    bool []present = new bool[MAX];     while (x > 0)    {         // Last digit of x        int digit = x % 10;         // If current digit has        // appeared before        if (present[digit])            return false;         // Mark the current digit        // to present        present[digit] = true;         // Remove the last digit        x /= 10;    }    return true;} // Function to return the// required value of kstatic int findK(int l, int r){     // To store the maximum value    // for the given expression    int maxExp = int.MinValue;    int k = -1;    for (int i = l; i <= r; i++)    {         // If i contains all distinct digits        if (distinctDigits(i))        {            int exp = (l - i) * (i - r);             // If the value of the expression            // is also maximum then update k            // and the expression            if (exp >= maxExp)            {                k = i;                maxExp = exp;            }        }    }    return k;} // Driver codepublic static void Main(String[] args){    int l = 50, r = 60;     Console.Write(findK(l, r));}} // This code is contributed by Rajput-Ji
<script>// javascript implementation of the approach   var MAX = 10;     // Function that returns true if x    // contains all distinct digits    function distinctDigits(x)    {        var present = new Array(MAX).fill(false);         while (x > 0) {             // Last digit of x            var digit = x % 10;             // If current digit has            // appeared before            if (present[digit])                return false;             // Mark the current digit            // to present            present[digit] = true;             // Remove the last digit            x = parseInt(x/10);        }         return true;    }     // Function to return the    // required value of k    function findK(l , r) {         // To store the maximum value        // for the given expression        var maxExp = Number.MIN_VALUE;        var k = -1;        for (var i = l; i <= r; i++) {             // If i contains all distinct digits            if (distinctDigits(i)) {                var exp = (l - i) * (i - r);                 // If the value of the expression                // is also maximum then update k                // and the expression                if (exp >= maxExp) {                    k = i;                    maxExp = exp;                }            }        }         return k;    }     // Driver code        var l = 50, r = 60;        document.write(findK(l, r)); // This code is contributed by gauravrajput1</script>
56
 
SURENDRA_GANGWAR
29AjayKumar
Rajput-Ji
GauravRajput1
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shortest path in a directed graph by Dijkstra’s algorithm
Breadth First Traversal ( BFS ) on a 2D array
Multistage Graph (Shortest Path)
Runtime Errors
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7