fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone {
  6. public static boolean isContiguous(long[] array) {
  7. Set <Long> hashset = new HashSet <Long> ();
  8. long min = array[0];
  9. long max = array[0];
  10. for (int i = 0; i < array.length; i++) {
  11. if (hashset.add(array[i]) == false) return false;
  12. if (min > array[i]) min = array[i];
  13. if (max < array[i]) max = array[i];
  14. }
  15. return max - min + 1 == array.length;
  16. }
  17. public static void main(String[] args) throws java.lang.Exception {
  18. System.out.println(isContiguous(new long[] { 1, 2, 3, 5, 6, 7 })); // false, missing 4
  19. System.out.println(isContiguous(new long[] { 1, 2, 3, 4, 4, 6, 7 })); // false, missing 5
  20. System.out.println(isContiguous(new long[] { 1, 2, 3, 5, 6, 7, 4 })); // true
  21. }
  22. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
false
false
true