MEENA_008 Editorial

Practice
Div-2 Contest
Div-1 Contest
Author: Yogesh Deolalkar
Tester: Ram Agrawal
Editorialist: Prathamesh Sogale

DIFFICULTY:

SIMPLE, EASY.

PREREQUISITES:

Math,HashMap,Sorting

PROBLEM:

In a college named Dr.Babasaheb Ambedkar Technological university, Lonere, new first-year admissions took place. Now each class has one class representative. Many students were interested to be class representative. So the Head of Department came up with an idea to hold elections. Students have to cast their votes by the name of the candidate they choose. Consider Names of candidates to be represented as numbers. Given an arr[i] Which represents votes cast by all students. print the number denoted as the name of the candidate who wins the election. One who got strictly more than half the votes will be considered as the winner of the election and will become the Class representative. If no candidate gets more than half the votes print “NOTA”

QUICK EXPLANATION:

If the occurance of the digit is greater than n/2 then print that number.

SOLUTIONS:

Setter's Solution

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

void fast_input()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}

int main() {
// your code goes here
fast_input();
int T;
int n;
const int N=10e5+2;
cin>>T;
while(T–)
{
int flag=0;
int mx=INT_MIN;
cin>>n;
int a[n];
int b[N]={0};
for(int i=0; i<n; i++)
{
cin>>a[i];
b[a[i]]++;
}

    for(int i=0; i<N; i++)
    {
        mx=max(mx,b[i]);
        if(mx>(n/2))
        {
            flag++;
            cout<<i<<endl;
            break;
        }
    }
    if(flag==0)
       cout<<"NOTA"<<endl;
    
}
return 0;

}

Tester's Solution

#include
using namespace std;

int main() {
int t,max;cin>>t;
while(t–)
{
int n,c=0;cin>>n;int a[n],b[100],h=0;
for(int i=0;i<n;i++)
cin>>a[i];
int m=0,k=-1;
for(int i=0;i<n;i++)
{
int c=0;
for(int j=0;j<n;j++)
{
if(a[i]==a[j])
c++;
}
if(c>m)
{
m=c;
k=i;
}
}
if(m>n/2)cout<<a[k]<<endl;
else cout<<“NOTA”<<endl;
}
return 0;
}