fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. static int n;
  5. static int[] a = new int[100002];
  6. static int[] f = new int[100002];
  7. public static int solve() {
  8. int ans = 0;
  9. int left = 0;
  10. int unique = 0;
  11. for(int right = 0; right < n; ++ right) {
  12. f[a[right]] += 1;
  13. if(f[a[right]] == 1) {
  14. unique += 1;
  15. }
  16. if(unique <= 2) {
  17. ans = Math.max(ans, right - left + 1);
  18. }
  19. else {
  20. while(unique > 2) {
  21. if(f[a[left]] == 1) {
  22. unique -= 1;
  23. }
  24. f[a[left]] -= 1;
  25. left += 1;
  26. }
  27. }
  28. }
  29. return ans;
  30. }
  31. public static void main(String[] args) {
  32. Scanner scan = new Scanner(System.in);
  33. n = scan.nextInt();
  34. for(int i = 0; i < n; ++ i) {
  35. a[i] = scan.nextInt();
  36. }
  37. System.out.println(solve());
  38. scan.close();
  39. }
  40. }
Success #stdin #stdout 0.13s 47748KB
stdin
5
1 2 3 3 2
stdout
4