fork(2) download
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class Ideone
  6. {
  7. public static void solve(Scanner sc, PrintStream out) {
  8. List<Integer> list = new ArrayList<>();
  9. while (sc.hasNext()) list.add(sc.nextInt());
  10.  
  11. int[] places = new int[list.size()];
  12. for (Integer i : list) ++places[i];
  13.  
  14. int a = 0, b = places.length - 1;
  15. while (!(a == b || --places[list.get(a)] == 0)) ++a;
  16. while (!(a == b || --places[list.get(b)] == 0)) --b;
  17.  
  18. out.println(b - a + 1);
  19. }
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. Scanner sc = new Scanner(System.in);
  24.  
  25. while (sc.hasNext()) {
  26. Scanner scLine = new Scanner(sc.nextLine());
  27. solve(scLine, System.out);
  28. scLine.close();
  29. }
  30.  
  31. sc.close();
  32. }
  33. }
Success #stdin #stdout 0.17s 321344KB
stdin
7 3 7 3 1 3 4 1
1 2 3 1 2 1
5 5 5 5 5 5
stdout
5
3
1