Count the Specials | Practice | GeeksforGeeks

Given an array A (may contain duplicates) of N elements and a positive integer K. The task is to count the number of elements which occurs exactly floor(N/K) times in the array.
Example:
Input:N = 5K = 2A[] = 1 4 1 2 4Output:2Explanation:In the given array, 1 and 4 occurs floor(5/2) = 2 times. So count is 2.
Your Task:Your task is to complete the function countSpecials() which should count the elements which occurs exactly floor(N/K) times.
Constrains:1 <= N <= 1031 <= Ai <= 1031 <= K <= 103
0
hgaur7011 month ago
TIME COMPLEXITY : O(n+m) , TIME TAKEN : 0.0
BY USING MAP :-
int countSpecials(int arr[], int sizeof_array, int K){
    
    int f = floor(sizeof_array/K), count = 0;
    
    int cot = 0;
    
    map<int, int> ob;
    map<int, int>::iterator it;

    for (int i = 0; i < sizeof_array; i++)
    {
        ob[arr[i]]++;
    }

    for (auto &&i : ob)
    {
        if (i.second == f)
        {
            count++;
        }
    }

    
    return count;
    
}
0
sumitd12991 month ago
SIMPLEST CODE WITH LEAST TIME COMPLEXITY YOU WILL GET HERE IS :
 
int countSpecials(int arr[], int sizeof_array, int K){      int f = floor(sizeof_array/K), count = 0;      int arr2[1000]={0};   // Your code here   for(int i=0;i<sizeof_array;i++){       arr2[arr[i]]++;   }   for(int i=0;i<1000;i++){       if(arr2[i]==f){           count++;       }   }
return count;
}
+1
benklomp2 months ago
int countSpecials(int arr[], int n, int K)
{
    int f = floor(n/K), count = 0;
    
    // Your code here
    
    // simple bubble sort
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            if(arr[i] > arr[j])
            {
                int T = arr[i];
                arr[i] = arr[j];
                arr[j] = T;
            }
            
    int counter = 0, p = -1;
    for(int i = 0; i < n; i += 1)
    {
        if(arr[i] == p) continue;
        
        int counter = 1, j = i;
        while(++j <  n && arr[i] == arr[j])
                ++counter;
                
        if(counter == f)
            ++count;
            
        p = arr[i];
    }
            
    return count;
0
sumitrodge272 months ago
i dont know how map works, someone help!
int countSpecials(int arr[], int sizeof_array, int K){
    
    int f = floor(sizeof_array/K), count = 0;
    int t=0;
    for(int i=0;i<sizeof_array;i++)
    {
        for(int j=0;j<sizeof_array;j++)
        {
            if(arr[i]==arr[j])
            {
                t++;
            }
        }
        if(t==f)
        {
            count++;
        }
        t=0;
    }
    
    count=ceil(count/f);
    
    return count;
    
}
0
sumitrodge273 months ago
someone please tell me what's wrong with my code 
 
int countSpecials(int arr[], int sizeof_array, int K){   int f = floor(sizeof_array/K), count = 0;   int p=0;   for(int i=0;i<sizeof_array;i++)   {              for(int j=0;j<sizeof_array;j++)       {           if(arr[i]==arr[j])           {              p+=1;            }       }       if(p==f)       {           count+=1;       }       p=0;   }   return count;   }
 
-2
dipawalimandaokar3213 months ago
// { Driver Code Starts//Initial Template for C++#include <bits/stdc++.h>using namespace std;
// Function Prototypeint countSpecials(int[], int, int);
// } Driver Code Ends//User function Template for C++
/*Function to count number of elements with occurrence* exactly equal to floor(sizeof_array/K)* arr : input array* sizeof_array : number of array elements*/int countSpecials(int arr[], int sizeof_array, int K){      int f = floor(sizeof_array/K),p=0;      for(int i=0;i<sizeof_array;i++)   {        int count;       for(int j=0;j<sizeof_array;j++)
    {   if(arr[i]==arr[j])       count++ ;    }         if(count==f)        p++;                 }   // Your code here            return p;   }
// { Driver Code Starts.
// Driver code to rest countSpecials functionint main() {// Testcase inputint testcase;cin >> testcase;while(testcase--){        // sizeof_array : number of array elements    // K : as given in statement    int sizeof_array, K;    cin >> sizeof_array >> K;        // array of size sizeof_array    int arr[sizeof_array];        for(int i = 0;i<sizeof_array;i++){        cin >> arr[i];    }        // calling function and printing count the occurrence    cout << countSpecials(arr, sizeof_array, K) << endl;    }return 0;}  // } Driver Code Ends
Whats the problem in countspecial function..am unable to understand.
0
hitentandon3 months ago
JS Sol:
        let f = Math.floor(N/K), count = 0, cc=1;
       arr.sort();
       for(let i = 1; i < N; i++)
           if(arr[i]==arr[i-1])
               cc++;
           else if(cc===f)
           {
               count++;
               cc=1;
           }
           else
               cc=1;
       if(cc===f)
           count++;
       return count;
C++ Sol:
   int f = floor(N/K), count = 0, cc=1;
   sort(arr, arr+N);
   for(int i = 1; i < N; i++)
       if(arr[i]==arr[i-1])
           cc++;
       else if(cc==f)
       {
           count++;
           cc=1;
       }
       else
           cc=1;
   if(cc==f)
       count++;
   return count;
+1
sourabhsingh191220014 months ago
Easy C++ Solution 
int countSpecials(int arr[], int sizeof_array, int K){
    
    int f = floor(sizeof_array/K), cnt = 0;
    unordered_map<int,int>mp;
    
    // Your code here
    for(int i=0;i<sizeof_array;i++){
        if(mp.find(arr[i]) == mp.end()){
            mp[arr[i]]=1;
        }else
       mp[arr[i]]++;
    }
    for(auto &i:mp){
        if(i.second==f)
        cnt++;
    }
    
    return cnt;
    
}
0
harshitd8914 months ago
int countSpecials(int arr[], int N, int K){   int f = floor(N/K), count = 0;int array[1000];for(int i=0;i<1000;i++){array[i]=0;   }
for(int i=0;i<1000;i++){   if(i<N){   int p=arr[i];   array[p]=array[p]+1;       }   else{       break;   }}for(int i=0;i<1000;i++){   if(array[i]==f){       count++;   }}   return count;   } 
0
ayushnautiyal11106 months ago
int countSpecials(int arr[], int sizeof_array, int K){      int f = floor(sizeof_array/K), count = 0;   unordered_map<int,int>mp;   for(int i=0;i<sizeof_array;i++){       mp[arr[i]]++;   }   for(auto it =mp.begin();it!=mp.end();it++){       if(it->second==f){           count++;       }   }   return count;}
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.