PROBLEM LINK:CodeChef: Practical coding for everyone DIFFICULTY: Cake-walk PREREQUISITES: Nothing EXPLANATION:
We just have to iterate from r to l (both inclusive) and find the value of (r-i)*(i-l) and keep track of maximum of this value and for which number it is maximum. SOLUTION: (Python 3)
defcheck(i):
d={}
for j in str(i):
if j notin d:
d[j]=1else:
return False
return True
l,r=map(int,input().split())
m=-1
ind=-1for i in range(r,l-1,-1):
if check(i) and (r-i)*(i-l)>m:
m=(r-i)*(i-l)
ind=i
print(ind)
what is wrong with the following code can anybody tell.Always one test case is failing I cant understand which one.
#include<bits/stdc++.h> #define llt long long int #define ullt unsigned long long int
using namespace std;
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int check(int x)
{
int arr[10]={0};
while(x>0)
{
int u=x%10;
arr[u]++;
if(arr[u]>1)
return 0;
x=x/10;
}
return 1;
}
int main()
{
fast();
int largest=0,ar=0,ans=-1;
//cin>>t;
int l,r;
cin>>l>>r;
for(int i=l;i<=r;i++)
{
if(check(i))
{
ar=(r-i)*(i-l);
if(ar>=largest){
largest=ar;
ans=i;}
}
}
cout<<ans;
Sorry for the late reply,
ur logic seems fine but I think it is overflowing due to integer.
If you had used long long int instead of int then it was surely going to pass.
Thanks