Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. final class Variables {
  2. private final int x;
  3. private final int y;
  4.  
  5. Variables(int x2, int y2) {
  6. this.x = x2;
  7. this.y = y2;
  8. }
  9.  
  10. public int getX() {
  11. return x;
  12. }
  13.  
  14. public int getY() {
  15. return y;
  16. }
  17.  
  18. @Override public String toString() {
  19. return "x = " + x + " y = " + y;
  20. }
  21. }
  22.  
  23.  
  24. public final class FindMissingRepeating {
  25.  
  26. /**
  27. * Takes an input a sorted array with only two variables that repeat once. in the
  28. * range. and returns two variables that are missing.
  29. *
  30. * If any of the above condition does not hold true at input the results are unpredictable.
  31. *
  32. * Example of input:
  33. * range: 1, 2, 3, 4
  34. * input array [1, 1, 4, 4]
  35. * the variables should contain 2 missing elements ie 2 and 3
  36. *
  37. * @param a : the sorted array.
  38. */
  39. public static Variables sortedConsecutiveTwoRepeating (int[] a, int startOfRange) {
  40. if (a == null) throw new NullPointerException("a1 cannot be null. ");
  41.  
  42. int low = startOfRange;
  43. int high = low + (a.length - 1);
  44.  
  45. int x = 0;
  46. int i = 0;
  47. boolean foundX = false;
  48.  
  49. while (i < a.length) {
  50. if (a[i] < low) {
  51. i++;
  52. } else {
  53. if (a[i] > low) {
  54. if (foundX) {
  55. return new Variables(x, low);
  56. } else {
  57. x = low;
  58. foundX = true;
  59. }
  60. }
  61. low++;
  62. i++;
  63. }
  64. }
  65.  
  66. int val = foundX ? x : high - 1;
  67. return new Variables(val, high);
  68. }
  69.  
  70.  
  71.  
  72. public static void main(String[] args) {
  73. int[] ar1 = {1, 2, 2, 3, 4, 4, 5};
  74. Variables v1 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar1, 1);
  75. System.out.println("Expected x = 6, y = 7 " + v1);
  76.  
  77. int[] ar2 = {2, 2, 4, 4, 5, 6, 7};
  78. Variables v2 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar2, 1);
  79. System.out.println("Expected x = 1, y = 3 " + v2);
  80.  
  81. int[] ar3 = {3, 3, 4, 4, 5, 6, 7};
  82. Variables v3 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar3, 1);
  83. System.out.println("Expected x = 1, y = 2 " + v3);
  84.  
  85. int[] ar4 = {1, 3, 3, 4, 4, 6, 7};
  86. Variables v4 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar4, 1);
  87. System.out.println("Expected x = 2, y = 5 " + v4);
  88. }
  89. }
  90.  
  91. public static ExVariables sortedConsecutiveTwoRepeating(int[] a,
  92. final int startOfRange) {
  93.  
  94. int low = startOfRange;
  95. int high = low + (a.length - 1);
  96.  
  97. int x = low - 1;
  98. boolean foundX = false;
  99.  
  100. for (int i = 0; i < a.length; i++) {
  101. if (a[i] > low) {
  102. if (foundX) {
  103. return new ExVariables(x, low);
  104. }
  105. x = low;
  106. foundX = true;
  107. }
  108. if (a[i] >= low) {
  109. low++;
  110. }
  111. }
  112.  
  113. if (foundX) {
  114. return new ExVariables(x, high);
  115. } else {
  116. return new ExVariables(high - 1, high);
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement