Kth Frequency | Practice | GeeksforGeeks

Geek hosted a coding competition, some of the questions were easy and some of them were hard. You are given an array arr of positive integers of size N and an integer K, arr[i] represents the hardness of each problem of geeks' contest. Among those N numbers, your task is to find the numbers which appear more than K times and print them in increasing order. If no number appears more than K times than print -1.
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated integers N and K.
3. The second line contains N space-separated positive integers represents array arr.

Output: For each test case, print the maximum number of chocolates you can collect

Constraints:
1. 1 <= T <= 10
2. 1 <= K, N <= 100000
3. 1 <= arr[i] <= 10^9

Example:
Input:
3
3 1
5 5 6
6 1
2 2 3 3 4 4
4 2
1 2 2 3
Output:
5
2 3 4
-1
Explanation:
Test case 1: The only problem with hardness 5 appears more than once
+1
avinav26113 months ago
Easy C++ Solution
 
+1
rohanpandey7493 months ago
Simple use of map and vector: 
#include<iostream>;
#include<bits/stdc++.h>;
using namespace std;
int main()
 {
	//code
    int t;
    cin >> t;
    while(t--){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        vector<int>answer;
        map<int,int>mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        for(auto it:mp){
            if(it.second>k){
                answer.push_back(it.first);
            }
        }
        if(answer.size()==0){
            answer.push_back(-1);
        }
        for(int i=0;i<answer.size();i++){
            cout << answer[i] << " ";
        }
        cout << "\n";
    }
	return 0;
}
0
am79041ak3 months ago
Don't use Freq Array, it gives runtime error(I think bcz of using more memory). Instead use a frequency map to get A.C. Here is my Java Code.
class GFG {
	public static void main (String[] args) {
		//code
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
		
		    Map<Integer,Integer> mp=new HashMap<>();
		    List<Integer> ans=new ArrayList<>();
		    
		    int n=sc.nextInt(); int k=sc.nextInt();
		    int[] arr = new int[n];
		    
		    for(int i=0;i<n;i++){
		        arr[i]=sc.nextInt();
		        if(mp.get(arr[i])==null) mp.put(arr[i],1);
		        else {
		            int pos=mp.get(arr[i]);
		            mp.put(arr[i],pos+1);
		        }
		    }
		    for(int key:mp.keySet()){
		        if(mp.get(key)>k) ans.add(key);
		    }
		    if(ans.size()==0) System.out.print(-1);
		    for(int e:ans) System.out.print(e + " ");
		    System.out.println();
		}
	}
}
0
imranwahid6 months ago
Easy C++ solution
+1
imranwahid6 months ago
Easy C++ solution
+1
badgujarsachin836 months ago
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
 {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n,k;
	    cin>>n>>k;
	    int arr[n];
	    for(int i=0;i<n;i++){
	        cin>>arr[i];
	    }
	    map<int,int> mp;
	    for(int i=0;i<n;i++){
	        mp[arr[i]]++;
	        
	    }
	    vector<int> v;
	    for(auto it:mp)
	    {
	        if(it.second>k){
	            v.push_back(it.first);
	            cout<<it.first<<" ";
	        }
	    }
	    if(v.empty()){
	        cout<<-1;
	    }
	    cout<<endl;
	}
	return 0;
}
-1
aakashrarya19977 months ago
Output: For each test case, print the maximum number of chocolates you can collect 
There is no concept related to chocolates ,this problem based on hardness of problem...
0
Aaryan Rawat7 months ago
Aaryan Rawat
in one of the Test Case input is 8 31 2 1 8 1 6 9 5
no value for t  what to do there ?
0
ANAS MALVAT9 months ago
ANAS MALVAT
int main() {//codeint T;cin >> T;while(T--){    int N, K;    cin >> N >> K;    int arr[N];    map <int,int> mp;    for(int i  = 0 ; i <  N ; i ++)    {        cin >> arr[i];        mp[arr[i]] ++;    }    int flag = 1;    for(auto i: mp)    {        if(i.second > K)        {            flag = 0;            cout<<i.first<<" ";="" }="" }="" if(flag)="" cout<<-1;="" cout<<endl;="" }="" return="" 0;="" <="" code="">
+1
soumik9 months ago
soumik
def kfreq(arr,n,k):     d={}    for num in arr:        d.setdefault(num,0)        d[num]+=1     res={key:value for (key,value) in d.items() if value>k}      if bool(res)==True:        print(*sorted(list(res)))    else:        print(-1)t=int(input())for i in range(t):    n,k=input().split()    n=int(n)    k=int(k)    arr=list(map(int,input().split()))    kfreq(arr,n,k)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.