Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD
DIFFICULTY:
CAKEWALK, SIMPLE, EASY.
PREREQUISITES:
Math .
PROBLEM:
Chintu 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 3 .
If 3rd largest element which is multiple of 3 is exist then print that Number else print −1
EXPLANATION:
In given array firstly we have to find out which number is multiple of 3 ,
if there exist such type of number then we have to print the 3rd largest number which is multiple of 3 if exist else print -1 .
for example
n=6
2 3 9 6 5 0
in above example 0 3 6 9 these numbers are multiple of three
but the 3rd largest element is 3 so the output is three
if there not exist 3rd largest element then output will be -1
SOLUTIONS:
Setter's Solution
-
//3rd largest
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
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]%3==0)
-
{
-
cnt++;
-
if(cnt==3)
-
{
-
cout<<a[i];
-
break;
-
}
-
}
-
}
-
if(cnt!=3)
-
{
-
cout<<“-1”;
-
}
- }
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}
Tester's Solution
-
//3rd largest
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
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]%3==0)
-
{
-
cnt++;
-
if(cnt==3)
-
{
-
cout<<a[i];
-
break;
-
}
-
}
-
}
-
if(cnt!=3)
-
{
-
cout<<“-1”;
-
}
- }
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}
Editorialist's Solution
-
//3rd largest
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
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]%3==0)
-
{
-
cnt++;
-
if(cnt==3)
-
{
-
cout<<a[i];
-
break;
-
}
-
}
-
}
-
if(cnt!=3)
-
{
-
cout<<“-1”;
-
}
- }
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}