### Question:
LeastDifferenceInArrayWithGivenNumber { public static int getElementWithLeastDifference(int[] sequence, int element) { if (sequence == null || sequence.length == 0) { throw new IllegalArgumentException("Input array is null or empty"); } int left = 0; int right = sequence.length - 1; while (left < right) { int median = left + ((right - left) >> 1); if (sequence[median] < element) { left = median + 1; } else { right = median; } } if (sequence[left] == element) { return element; } else { if (left == 0) { return sequence[0]; } else { if (abs(element - sequence[left]) >= abs(element - sequence[left - 1])) { return sequence[left - 1]; } else { return sequence[left]; } } } }  static int getElementWithLeastDifference(int[] sequence, int element);  }### Answer:
@Test(expected = IllegalArgumentException.class) public void testGetElementWithLeastDifferenceNullArray() { getElementWithLeastDifference(null, 1); }
@Test(expected = IllegalArgumentException.class) public void testGetElementWithLeastDifferenceEmptyArray() { getElementWithLeastDifference(new int[0], 1); }
@Test public void testGetElementWithLeastDifference1() { int actual = getElementWithLeastDifference(new int[]{1, 3, 5, 7, 9, 11, 13, 15}, 1); assertThat(actual, is(1)); }
@Test public void testGetElementWithLeastDifference2() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 2); assertThat(actual, is(1)); }
@Test public void testGetElementWithLeastDifference3() { int actual = getElementWithLeastDifference(new int[]{1, 3, 5, 7, 9, 11, 13, 15}, 2); assertThat(actual, is(1)); }
@Test public void testGetElementWithLeastDifference4() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 2); assertThat(actual, is(1)); }
@Test public void testGetElementWithLeastDifference5() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 5); assertThat(actual, is(5)); }
@Test public void testGetElementWithLeastDifference6() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 14); assertThat(actual, is(13)); }
@Test public void testGetElementWithLeastDifference7() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 15); assertThat(actual, is(15)); }
@Test public void testGetElementWithLeastDifference8() { int actual = getElementWithLeastDifference(new int[]{1, 4, 5, 7, 9, 11, 13, 15}, 20); assertThat(actual, is(15)); }