Header Ad

CODECHEF Dominant Element Problem Solution (DOMINANT2)

In this CodeChef dominant element problem solution, You are given an array AA of length NN. An element XX is said to be dominant if the frequency of XXin AA is strictly greater than the frequency of any other element in the AA.For example, if A = [2, 1, 4, 4, 4]A=[2,1,4,4,4] then 44 is a dominant element since its frequency is higher than the frequency of any other element in AA.

Find if there exists any dominant element in AA.

CODECHEF Dominant Element Problem Solution (DOMINANT2)

CODECHEF Dominant Element Problem Solution ( DOMINANT2)

//Divyansh Vinayak
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d",x)
#define pl(x)	printf("%lld",x)
#define ps(s)	printf("%s",s)
#define pnl()   printf("\n")
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
int mpow(int base, int exp); 
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 18 * 103, M = N;
#define top(a,b) (ll)((a+b-1)/b)
//=======================
vi g[N];
void matrix(ll n, ll m){
  char s[101][101];
    while(cin>>n>>m){
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>s[i][j];
            }
        }
}
}

int fact(ll n);
 

 
// Returns factorial of n
int fact(ll n)
{
    ll res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}

int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int main() {
  ll t;
  cin >> t;
  while(t--){
    ll n;
    cin >> n;
    vi a;
    vi b;
    for(int i = 0; i < n; i++)
    {
        ll num;
        cin >> num;
        a.push_back(num);
    }
    set<int> s;
    for(int x:a){
        s.insert(x);
    }
    for(int x:s){
        b.push_back(x);
    }
    ll count1 = 0;
    ll max_count = 0;
    for(int i = 0; i < b.size(); i++){
        ll temp_count;
        temp_count = count(a.begin(),a.end(),b[i]);
        
        if(temp_count > count1 && temp_count > max_count){
            max_count = temp_count;
            count1 = 1;
        }
        else if(temp_count == max_count){
            count1 = 0;
        }
        
    }
    if(count1)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;

  }
  return 0;
}      


int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}


Post a Comment

0 Comments