Third largest number

Practice

Author: Anurag
Tester: Anurag
Editorialist: Anurag

DIFFICULTY:

EASY,MEDIUM,GREEDY,STRING,CAKEWALK, SIMPLE

PREREQUISITES:

Math ,String

PROBLEM:

Chef is very intelligent in maths his teacher given him a work , he has given N number of element and he has to find 3rd largest element in that number which is multiple of 5 .

If 3rd largest element which is multiple of 5 is exist then print that Number else print −1

EXPLANATION:

In given array firstly we have to find out which number is multiple of 5 ,
if there exist such type of number then we have to print the 3rd largest number which is multiple of 5 if exist else print -1 .
for example
n=6
2 5 15 10 9 0

in above example 0 5 10 15 these numbers are multiple of three
but the 3rd largest element is 5 so the output is three
if there not exist 3rd largest element then output will be -1

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int cnt=0;
for(int i=n-1;i>=0;i–)
{
if(a[i]%5==0)
{
cnt++;
if(cnt==3)
{
cout<<a[i]<<endl;
break;
}
}
}
if(cnt!=3)
{
cout<<“-1”<<endl;
}

}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int cnt=0;
for(int i=n-1;i>=0;i–)
{
if(a[i]%5==0)
{
cnt++;
if(cnt==3)
{
cout<<a[i]<<endl;
break;
}
}
}
if(cnt!=3)
{
cout<<“-1”<<endl;
}

}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int cnt=0;
for(int i=n-1;i>=0;i–)
{
if(a[i]%5==0)
{
cnt++;
if(cnt==3)
{
cout<<a[i]<<endl;
break;
}
}
}
if(cnt!=3)
{
cout<<“-1”<<endl;
}

}
return 0;
}