DisplayAbsentValues.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. public class DisplayAbsentValues {
  4. public static void main(String[] args) {
  5. System.out.println();
  6. // Array 1
  7. int[] arr1 = {0, 1, 2, 50, 52, 75};
  8. PrintArray(arr1);
  9. System.out.println(DisplayAbsentValues(arr1));
  10. System.out.println();
  11. // Array 2
  12. int[] arr2 = {1, 2, 50, 53, 75, 99};
  13. PrintArray(arr2);
  14. System.out.println(DisplayAbsentValues(arr2));
  15. System.out.println();
  16. // Array 3
  17. int[] arr3 = {2, 50, 52, 75, 98};
  18. PrintArray(arr3);
  19. System.out.println(DisplayAbsentValues(arr3));
  20. System.out.println();
  21. // Array 4
  22. int[] arr4 = {10, 20, 30, 40, 50};
  23. PrintArray(arr4);
  24. System.out.println(DisplayAbsentValues(arr4));
  25. System.out.println();
  26. // Array 5
  27. int[] arr5 = {10, 20, 30, 40, 50, 98, 99};
  28. PrintArray(arr5);
  29. System.out.println(DisplayAbsentValues(arr5));
  30. System.out.println();
  31. // Array 6
  32. int[] arr6 = {10, 20, 30, 40, 50, 97, 99};
  33. PrintArray(arr6);
  34. System.out.println(DisplayAbsentValues(arr6));
  35. System.out.println();
  36. // Array 7
  37. int[] arr7 = {10, 20, 30, 40, 50, 96, 99};
  38. PrintArray(arr7);
  39. System.out.println(DisplayAbsentValues(arr7));
  40. System.out.println();
  41. // Array 8
  42. int[] arr8 = {10, 20, 30, 40, 50, 95, 99};
  43. PrintArray(arr8);
  44. System.out.println(DisplayAbsentValues(arr8));
  45. System.out.println();
  46. // Array 9
  47. int[] arr9 = {0, 1, 10, 20, 30, 40, 50};
  48. PrintArray(arr9);
  49. System.out.println(DisplayAbsentValues(arr9));
  50. System.out.println();
  51. // Array 10
  52. int[] arr10 = {0, 2, 10, 20, 30, 40, 50};
  53. PrintArray(arr10);
  54. System.out.println(DisplayAbsentValues(arr10));
  55. System.out.println();
  56. // Array 11
  57. int[] arr11 = {0, 3, 10, 20, 30, 40, 50};
  58. PrintArray(arr11);
  59. System.out.println(DisplayAbsentValues(arr11));
  60. System.out.println();
  61. // Array 12
  62. int[] arr12 = {0, 4, 10, 20, 30, 40, 50};
  63. PrintArray(arr12);
  64. System.out.println(DisplayAbsentValues(arr12));
  65. }
  66. public static void PrintArray(int[] input) {
  67. System.out.print("{");
  68. for (int i = 0; i < input.length; ++i) {
  69. System.out.print(input[i] + ", ");
  70. }
  71. System.out.println("\b\b}");
  72. }
  73. public static String DisplayAbsentValues(int[] input) {
  74. String result = "";
  75. int length;
  76. for (length = 1; length < input.length; ++length) {
  77. // This breaks out of the loop one pass too early, so increment length afterward
  78. if (length == input.length - 1 || input[length + 1] == 0) break;
  79. }
  80. length++;
  81. ArrayList<Integer> absent = new ArrayList<Integer>();
  82. for (int ctr = 0; ctr < 100; ++ctr) {
  83. if (Arrays.binarySearch(input, ctr) < 0) {
  84. absent.add(ctr);
  85. }
  86. }
  87. for (int ctr = 0; ctr < absent.size(); ++ctr) {
  88. if ((ctr < (absent.size() - 2)) && (absent.get(ctr + 2) == absent.get(ctr) + 2)) {
  89. result += absent.get(ctr);
  90. while ((ctr < (absent.size() - 2)) && (absent.get(ctr + 2) == absent.get(ctr) + 2)) {
  91. ctr++;
  92. }
  93. result += "-" + absent.get(ctr + 1);
  94. ctr++;
  95. } else {
  96. result += absent.get(ctr);
  97. }
  98. if (ctr < absent.size() - 1) {
  99. result += ",";
  100. }
  101. }
  102. return result;
  103. }
  104. }