Largest Difference(https://www.codechef.com/CBST2021/problems/DEFFC)

Practice

Author: noob_tech
Tester: noob_tech
Editorialist: noob_tech

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

You are given a number N. You have to find the maximum differences between two digit of given number. NOTE-(Number(N) of digit will be greater than two)

QUICK EXPLANATION:

Consider N= 246710, Here First find the largest digit which is 7 then find smallest digit from given N which is 0 .
So the largest difference will be 7.

EXPLANATION:

Here,We are given a number N. We have to find the Maximum difference between two digit of given number.
First we have to find the largest digit and smallest digit from the given Number.After that find the difference between largest digit and smallest digit we will get maximum difference between two number.
Note if N=0000 then answer must be 0.

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int k=15;
int main()
{
int t;
cin>>t;
while(t- -)
{
long long n;
cin>>n;
int a[k]={-1};
int i=0,cnt=0;
while(n>0)
{
int k=n%10;
if(k==0)
{
cnt=1;
}
a[i++]=k;
n=n/10;
}
sort(a,a+k);
int min;
for(i=0;i<k;i++)
{
if(cnt!=0)
{
if(a[i]==-1)
{
continue;
}
else{
min=a[i];
break;
}
}
else{
if(a[i]==-1 ||a[i]==0)
{
continue;
}
else
{
min=a[i];
break;
}
}
}
cout<<(a[k-1]-min)<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int k=15;
int main()
{
int t;
cin>>t;
while(t- -)
{
long long n;
cin>>n;
int a[k]={-1};
int i=0,cnt=0;
while(n>0)
{
int k=n%10;
if(k==0)
{
cnt=1;
}
a[i++]=k;
n=n/10;
}
sort(a,a+k);
int min;
for(i=0;i<k;i++)
{
if(cnt!=0)
{
if(a[i]==-1)
{
continue;
}
else{
min=a[i];
break;
}
}
else{
if(a[i]==-1 ||a[i]==0)
{
continue;
}
else
{
min=a[i];
break;
}
}
}
cout<<(a[k-1]-min)<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int k=15;
int main()
{
int t;
cin>>t;
while(t- -)
{
long long n;
cin>>n;
int a[k]={-1};
int i=0,cnt=0;
while(n>0)
{
int k=n%10;
if(k==0)
{
cnt=1;
}
a[i++]=k;
n=n/10;
}
sort(a,a+k);
int min;
for(i=0;i<k;i++)
{
if(cnt!=0)
{
if(a[i]==-1)
{
continue;
}
else{
min=a[i];
break;
}
}
else{
if(a[i]==-1 ||a[i]==0)
{
continue;
}
else
{
min=a[i];
break;
}
}
}
cout<<(a[k-1]-min)<<endl;
}
return 0;
}