fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int[] arr = new int[]{2,4,6,6,4,2,4};
  14. int n = arr.length;
  15.  
  16. System.out.println(helper(arr,n));
  17. }
  18. static int helper(int[] arr, int n){
  19.  
  20. HashMap<Integer,Integer> map = new HashMap<>();
  21. int cnt = 0;
  22. for(int i=0;i<n;i++){
  23. map.put(arr[i], map.getOrDefault(arr[i],0)+1);
  24. }
  25. for(Map.Entry<Integer,Integer> entry : map.entrySet()){
  26. int freq = entry.getValue();
  27. if(freq == 1){
  28. return -1;
  29. } else {
  30. cnt+= freq/3+(freq%3==0?0:1);
  31. }
  32. }
  33. return cnt;
  34.  
  35. }
  36. }
Success #stdin #stdout 0.08s 54568KB
stdin
Standard input is empty
stdout
3