Count the Specials - Java | Practice | GeeksforGeeks

Given an arry 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 1:
Input:
5 2
1 4 1 2 4
Output:
2
Explanation:
In the given array, 1 and 
4 occurs floor(5/2) = 2 
times. So count is 2.
Example 2:
Input:
3 2
99 66 66 
Output:
1
Explanation:
In the given array, 99 occurs 
floor(3/2) = 1 time. So count 
is 1.
Your Task:
You don't have to read input or print anything. Your task is to complete the function countSpecials() which should count the elements which occurs exactly floor(N/K) times.
Constrains:
1 <= N <= 103
1 <= Ai <= 103
 
0
jayesh291 month ago
Java 0.72 sec -
    static void countSpecials(int arr[], int n, int k){
        int f = (int)Math.floor(n/k);
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<n;i++){
            map.put(arr[i],map.getOrDefault(arr[i],0)+1);
        }
        int count=0;
        for (Map.Entry <Integer,Integer> mapElement : map.entrySet()) {
            int temp = mapElement.getValue();
            if(temp==f) count++;
        }
        System.out.println(count);
    }
0
ervishnu19942 months ago
class Geeks{   static void countSpecials(int a[], int n, int k){       int f = (int)Math.floor(n/k);       int result=0;       boolean b[]=new boolean[n];       for(int i=0;i<n;i++){           if(!b[i]){               int count =1;               for(int j=i+1;!b[i]&&j<n;j++){                   if(a[i]==a[j]){                       count++;                       b[j]=true;                   }                  }               if(count==f)result++;           }       }       System.out.println(result);   }}
0
kumarjay192602 months ago
Why am i not getting the correct output?plase help me.
class Geeks{
   static void countSpecials(int a[], int n, int k){
       int f = (int)Math.floor(n/k);
       
       int temp = 0;
       // your code here
       for(int i=0; i<n; i++){
           int count = 0;
           for(int j=i; j<n; j++){
               if(a[i] == a[j])
                  count++; 
           }
           if(count == f){
               temp++;
           }
       }
       System.out.println(temp);
   }
}
 
 
+1
shakirmalik707862 months ago
****************EASY CODE***************
 
static void countSpecials(int a[], int n, int k){       int f = (int)Math.floor(n/k);       // your code here       int count = 0;       for(int i=0; i<n; i++){           int rep = 0;           for(int j=0; j<n; j++){               if(a[i] == a[j]){                   rep++;               }           }           if(rep == f){               count++;           }       }       System.out.println(count/f);   }
0
sj0162672 months ago
static void countSpecials(int a[], int n, int k){       int f = (int)Math.floor(n/k);       int count=0;       // your code here       HashMap<Integer,Integer> hmap=new HashMap<>();       for(int i=0;i<n;i++)       {          hmap.put(a[i],hmap.getOrDefault(a[i],0)+1);                  }       for(Map.Entry<Integer,Integer> e:hmap.entrySet()){           if(e.getValue()==f)           count++;       }       System.out.println(count);   }
0
piyushchpkumar3 months ago
Easiest Simple code in java using function
 
public class Geeks{      public static int count(int a[], int i){       int ans =0;       for(int j =0;j<a.length;j++){           if(a[j]==i){               ans++;           }       }       return ans;   }         public static void countSpecials(int a[], int n, int k){              int t = (int)Math.floor(n/k);       int ans =0;       for(int i =0; i<n;i++){                      int c =count(a,a[i]);           if(c==t){                              ans++;           }        }       System.out.println(ans/t);  
// as complete array is traversed and each reapeted elements are counted seperately
   }}
0
ramprasana3 months ago
Hello Geeks,
 
I have a problem, my code works fine for the given example and sample input that I generate but when I submit it, the code is giving an odd output. I have only one print statement but it prints random digits. I also tried the same input that the test case was failing using the test against custom input I am able to get the expected out. Please help what is I am doing wrong. I am getting the same issue with a couple of other exercises as well. 
 
 
Thanks
Ram
 
-1
shaikrabnawaz786
This comment was deleted.
+1
shubhamkhuntia5 months ago
static void countSpecials(int a[], int n, int k){
        int f = (int)Math.floor(n/k);
        // your code here
        int ans=0;
        for(int i=0;i<n;i++){
            int count=0;
            for(int j=0;j<n;j++){
                if (a[i]==a[j]){
                    count++;
                }
            }
            if(count==f){
                ans++;
                a[i]=-1;
            }
        }
        System.out.println(ans);
    }
0
mimanshu16311626 months ago
class Geeks{   static void countSpecials(int a[], int n, int k){       int f = (int)Math.floor(n/k);       // your code here       int count = 0;       HashMap<Integer, Integer> hMap = new HashMap<>();       for(int i = 0; i< n; i++){           if(hMap.containsKey(a[i])){               hMap.replace(a[i] , hMap.get(a[i]) +1);           }else{               hMap.put(a[i], 1);           }       }       for(Map.Entry<Integer, Integer> e: hMap.entrySet()){           if(e.getValue()==f)               ++count;       }       System.out.println(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.