给定两个整数L和R ,其中L≤R ,任务是找到一个整数K ,使得:
- L≤K≤R 。
- K的所有数字都是唯一的。
- 表达式(L-K)*(K-R)的值最大。
如果存在多个答案,则为K选择较大的值。
例子:
Input: L = 5, R = 10
Output: 8
Input: L = 50, R = 60
Output: 56
方法:从L到R进行迭代,对于K的每个值,检查它是否包含所有不同的数字,并且(L-K)*(K-R)最大。如果两个或多个值为表达式给出相同的最大值,则为K选择较大的值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 10;
// Function that returns true if x
// contains all distinct digits
bool 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 k
int 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 code
int main()
{
int l = 50, r = 60;
cout << findK(l, r);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAX = 10;
// Function that returns true if x
// contains all distinct digits
static 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 k
static 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 code
public static void main(String[] args)
{
int l = 50, r = 60;
System.out.print(findK(l, r));
}
}
// This code is contributed by 29AjayKumar
Python 3
# Python3 implementation of the approach
import sys
MAX = 10
# Function that returns true if x
# contains all distinct digits
def 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 k
def 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 code
if __name__ == '__main__':
l = 50
r = 60
print(findK(l, r))
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX = 10;
// Function that returns true if x
// contains all distinct digits
static 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 k
static 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 code
public static void Main(String[] args)
{
int l = 50, r = 60;
Console.Write(findK(l, r));
}
}
// This code is contributed by Rajput-Ji
输出:
56