Editorial for MRID

PROBLEM LINK:CodeChef: Practical coding for everyone

Practice

Author: harsh_beniwal
Tester: harsh_beniwal
Editorialist: harsh_beniwal

DIFFICULTY:

EASY.

PREREQUISITES:

Math,Arrays.

PROBLEM:

Given an Array of Size N. Find The 5th Largest Element in the array.

If no 5th largest Element exists Then print -1.

QUICK EXPLANATION:

Firstly check if the array size is greater than or equal to 5 or not. If the array size is less than 5 the simply print ā€œ-1ā€.

if the array size is greater than or equal to 5.
Then Sort the array in Reverse Order and print the element present on the 4th index. i.e 5th element.

EXPLANATION:

Firstly check if the array size is greater than or equal to 5 or not. If the array size is less than 5 the simply print ā€œ-1ā€.

If the array size is greater than or equal to 5.
Then Sort the array in Reverse Order and print the element present on the 4th index. i.e 5th element.
We can sort the array in reverse order using any sorting algorithm as the testcase are max up to 10^6.

Note:- For C++ you can use the inbuilt STL (Standard Templet Library).

which is written as [ sort(a,a+n,greater()); ]

where ā€œaā€ is the name of the array and ā€œnā€ is the size of array. for using this STL you should must Include the header File i.e #include<bits/stdc++.h>

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,greater<int>());

    if(n<=4)

    {

        cout<<-1<<"\n";

    }

    else

    {

        cout<<a[4]<<"\n";

    }   

}

}

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,greater<int>());

    if(n<=4)

    {

        cout<<-1<<"\n";

    }

    else

    {

        cout<<a[4]<<"\n";

    }   

}

}

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,greater<int>());

    if(n<=4)

    {

        cout<<-1<<"\n";

    }

    else

    {

        cout<<a[4]<<"\n";

    }   

}

}

1 Like