Dominant Element Codechef Solution

Hello Programmers In this post, you will know how to solve the Dominant Element Codechef Solution. The Problem Code is DOMINANT2.

Dominant Element Codechef Solution
Dominant Element Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

You are given an array A of length N. An element X is said to be dominant if the frequency of X in A is strictly greater than the frequency of any other element in the A.

For example, if A = [2, 1, 4, 4, 4] then 4 is a dominant element since its frequency is higher than the frequency of any other element in A.

Find if there exists any dominant element in A.

Ezoicreport this adInput Format

  • The first line of input contains a single integer TT — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer NN — the size of the array AA.
  • The second line of each test case contains NN space-separated integers A_1, A_2, \ldots, A_NA1​,A2​,…,AN​ denoting the array AA.

Output Format

For each test case, output YES if there exists any dominant element in A. Otherwise, output NO.

You may print each character of YES and NO in uppercase or lowercase (for example, yesyEsYes will be considered identical).

Constraints

  • 1 ≤ T ≤ 500
  • 1 ≤ N ≤ 1000
  • 1 ≤ Ai​ ≤ N

Sample 1:

Input

4
5
2 2 2 2 2
4
1 2 3 4
4
3 3 2 1
6
1 1 2 2 3 4

Output

YES
NO
YES
NO

LinkedIn Skill Assessment Answers

Coursera Quiz Answers

Explanation:

Test case 1 : 2 is the dominant element.

Test case 2: There does not exist any dominant element.

Test case 3: 3 is the dominant element.

Dominant Element Codechef Solutions in CPP

#include <bits/stdc++.h>
using namespace std;
int main() {
 // your code goes here
 int t;
 cin>>t;
 while(t--) {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0; i<n; i++){
         cin>>arr[i];
     }
     vector<int> v;
     unordered_map<int, int> m;
     for(int i=0; i<n; i++){
        m[arr[i]]++;
     }
     for(auto it: m) v.push_back(it.second);
     sort(v.begin(), v.end());
     if(v[v.size()-1] > v[v.size()-2]) cout<<"YES"<<endl;
     else cout<<"NO"<<endl;
 }
 return 0;
}

Dominant Element Codechef Solutions in Python

# cook your dish here
z=int(input())
for i in range(z):
    n=int(input())
    l=list(map(int,input().split()))
    h={}
    for i in range(len(l)):
        if l[i] not in h:
            h[l[i]]=1
        else:
            h[l[i]]+=1
    l2=[]
    for keys,values in h.items():
        l2.append(values)
    a=max(l2)
    if l2.count(a)>1:
        print("NO")
    else:
        print("YES")

Dominant Element Codechef Solutions in JAVA

/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
  public static void main (String[] args) throws java.lang.Exception{
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    while(T-->0){
        int n = sc.nextInt();
        int[] input = new int[n];
        for(int i=0;i<n;i++){
            input[i] = sc.nextInt();
        }
        Map<Integer,Integer> mp = new HashMap<>();
        for(int x: input){
            mp.put(x, mp.getOrDefault(x,0)+1);
        }
        ArrayList<int[]> l = new ArrayList<>();
        for(int k : mp.keySet()){
            int v = mp.get(k);
            l.add(new int[]{k,v});
        }
        Collections.sort(l, (o1,o2)->o2[1]-o1[1]);
        if(l.size()==1 || l.get(0)[1] > l.get(1)[1]){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }
    }
  }
}
Ezoicreport this ad

Disclaimer: The above Problem (Dominant Element ) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Make A and B equal Codechef Solution

Sharing Is Caring

Leave a Comment