fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int[] nums = new int[n];
  8.  
  9. for (int i = 0; i < n; i++) {
  10. nums[i] = sc.nextInt();
  11. }
  12.  
  13. Set<Integer> numSet = new HashSet<>();
  14. for (int num : nums) {
  15. numSet.add(num);
  16. }
  17.  
  18. int longestSequence = 0;
  19.  
  20. for (int num : nums) {
  21. if (!numSet.contains(num - 1)) {
  22. int currentNum = num;
  23. int currentLength = 1;
  24.  
  25. while (numSet.contains(currentNum + 1)) {
  26. currentNum++;
  27. currentLength++;
  28. }
  29.  
  30. longestSequence = Math.max(longestSequence, currentLength);
  31. }
  32. }
  33.  
  34. System.out.println(longestSequence);
  35. }
  36. }
  37.  
Success #stdin #stdout 0.15s 56504KB
stdin
10
0 3 7 2 5 8 4 6 0 1
stdout
9