ENCJMAY4 - Editorial

PROBLEM LINK:

Practice
Contest Link

Author: Anjali Jha
Tester: Arnab Chanda

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Familiarity with Array

PROBLEM:

N integers, whose values lie between 1 to 5, have to be taken as input and the integer with the highest frequency needs to be printed

EXPLANATION:

An array freq is declared with length=6 and initialized with 0. Each index of freq will act as the counter for corresponding input integers which range from 1 to 5.
Run a loop from 0 to N and store the frequency of the input integers in freq.
Find the highest frequency and its corresponding index and print the index.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FORR(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define LL long long
#define L long
#define I int

using namespace std;
int main() {
    I t;
    cin>>t;
    while(t--)
    {
        I n,i;
        cin>>n;
        vector<I> a(n,0);
        vector<I> freq(6,0);
    
        FOR(i,0,n)
        cin>>a[i];
		 
        FOR(i,0,n)
        freq[a[i]]++;  

        I m=freq[1];
        I idx=1;
        FOR(i,2,6)
        {
            if(freq[i]>=m)
            {
                m=freq[i];
                idx=i;
            }
        }
        cout<<idx<<endl;
    }
    return 0;
}
2 Likes