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.
If multiple answers exist then choose the larger value for K.
Examples:
Input: L = 5, R = 10
Output: 8
Input: 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++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 10;
bool distinctDigits( int x)
{
bool present[MAX] = { false };
while (x > 0) {
int digit = x % 10;
if (present[digit])
return false ;
present[digit] = true ;
x /= 10;
}
return true ;
}
int findK( int l, int r)
{
int maxExp = INT_MIN;
int k = -1;
for ( int i = l; i <= r; i++) {
if (distinctDigits(i)) {
int exp = (l - i) * (i - r);
if ( exp >= maxExp) {
k = i;
maxExp = exp ;
}
}
}
return k;
}
int main()
{
int l = 50, r = 60;
cout << findK(l, r);
return 0;
}
|
Java
class GFG
{
static int MAX = 10 ;
static boolean distinctDigits( int x)
{
boolean []present = new boolean [MAX];
while (x > 0 )
{
int digit = x % 10 ;
if (present[digit])
return false ;
present[digit] = true ;
x /= 10 ;
}
return true ;
}
static int findK( int l, int r)
{
int maxExp = Integer.MIN_VALUE;
int k = - 1 ;
for ( int i = l; i <= r; i++)
{
if (distinctDigits(i))
{
int exp = (l - i) * (i - r);
if (exp >= maxExp)
{
k = i;
maxExp = exp;
}
}
}
return k;
}
public static void main(String[] args)
{
int l = 50 , r = 60 ;
System.out.print(findK(l, r));
}
}
|
Python 3
import sys
MAX = 10
def distinctDigits(x):
present = [ False for i in range ( MAX )]
while (x > 0 ):
digit = x % 10
if (present[digit]):
return False
present[digit] = True
x = x / / 10
return True
def findK(l, r):
maxExp = - sys.maxsize - 1
k = - 1
for i in range (l, r + 1 , 1 ):
if (distinctDigits(i)):
exp = (l - i) * (i - r)
if (exp > = maxExp):
k = i;
maxExp = exp
return k
if __name__ = = '__main__' :
l = 50
r = 60
print (findK(l, r))
|
C#
using System;
class GFG
{
static int MAX = 10;
static bool distinctDigits( int x)
{
bool []present = new bool [MAX];
while (x > 0)
{
int digit = x % 10;
if (present[digit])
return false ;
present[digit] = true ;
x /= 10;
}
return true ;
}
static int findK( int l, int r)
{
int maxExp = int .MinValue;
int k = -1;
for ( int i = l; i <= r; i++)
{
if (distinctDigits(i))
{
int exp = (l - i) * (i - r);
if (exp >= maxExp)
{
k = i;
maxExp = exp;
}
}
}
return k;
}
public static void Main(String[] args)
{
int l = 50, r = 60;
Console.Write(findK(l, r));
}
}
|
Javascript
<script>
var MAX = 10;
function distinctDigits(x)
{
var present = new Array(MAX).fill( false );
while (x > 0) {
var digit = x % 10;
if (present[digit])
return false ;
present[digit] = true ;
x = parseInt(x/10);
}
return true ;
}
function findK(l , r) {
var maxExp = Number.MIN_VALUE;
var k = -1;
for ( var i = l; i <= r; i++) {
if (distinctDigits(i)) {
var exp = (l - i) * (i - r);
if (exp >= maxExp) {
k = i;
maxExp = exp;
}
}
}
return k;
}
var l = 50, r = 60;
document.write(findK(l, r));
</script>
|
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 geeks!
Last Updated :
14 Sep, 2021
Like Article
Save Article