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. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int arr[] = new int[n];
  15. for(int i=0;i<n;i++){
  16. arr[i]=sc.nextInt();
  17. }
  18. Map<Integer,Integer> mp = new HashMap<>();
  19. for(int i=0;i<n;i++){
  20. mp.put(arr[i],mp.getOrDefault(arr[i],0)+1);
  21. }
  22. int noOfTrips = 0;
  23. for(int i:mp.keySet()){
  24. if(mp.get(i)==1){
  25. noOfTrips=-1;
  26. break;
  27. }
  28. noOfTrips = noOfTrips + (mp.get(i)/3);
  29. if(mp.get(i)%3!=0){
  30. noOfTrips++;
  31. }
  32. }
  33. System.out.println(noOfTrips);
  34. }
  35. }
Success #stdin #stdout 0.14s 56464KB
stdin
7
1 8 5 8 5 1 1
stdout
3