CH01SN-EDITORIAL

PROBLEM LINK :

Practice
Contest

Author : sneha542
Tester : prasoonjain006
Editorialist : sneha542

DIFFICULTY :

Cakewalk

PREREQUISITES :

Arrays

PROBLEM :

Chef has N numbers and he wants to choose one number from these numbers in such a way that the chosen number has occurred at least more than once and is greater than other repeated numbers.
Output 0 if there is No repeating number.

QUICK EXPLANATION :

Find out the greatest number among all the numbers which is repeating.

EXPLANATION :

→ First sort the array in descending order.

→ Now, check if the greatest number is repeating or not. If it is repeating then print it otherwise
check for the next highest number and so on.
→ If there is no repeating number then output 0 in this case.

TIME COMPLEXITY :
Time complexity is O(NlogN+N)

SOLUTIONS :

Editorialist’s Solution

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t>0) {
int n;
cin>>n;
int arr[n];
// take input
for(int i=0;i<n;i++){
cin>> arr[i];
}

//sort the array in descending order
sort(arr, arr+n, greater<int>());

//check for the first repeated element
if(n==1)
{
    cout<< 0 << "\n";
}
else if(n>1) {
    
   for(int i=0;i<n;i++){
  
     if (arr[i]== arr[i+1]){
         cout<< arr[i] << "\n";
         break;
       
      }
      
      else
         cout<< 0<< "\n";
   }

}

t--;

}

return 0;

}

Feel Free to share and discuss your approach here. :smiley: