### Question:
FindNthMaxNumber { public static int findNthMaxNumberUsingBinaryHeap(int[] sequence, int n) { if (sequence == null) { throw new IllegalArgumentException("Input array is null"); } if (n < 1) { throw new IllegalArgumentException("Input n is non-positive"); } if (sequence.length < n) { throw new IllegalArgumentException("Array size is less than input n"); } PriorityQueue<Integer> binaryHeap = new PriorityQueue<>(n); for (int element : sequence) { if (binaryHeap.size() < n) { binaryHeap.add(element); } else { Integer min = binaryHeap.peek(); if (min < element) { binaryHeap.poll(); binaryHeap.add(element); } } } return binaryHeap.peek(); }  static int findNthMaxNumberUsingSelectionSearch(int[] sequence, int n); static int findNthMaxNumberUsingBinaryHeap(int[] sequence, int n);  }### Answer:
@Test(expected = IllegalArgumentException.class) public void testFindNthMaxNumberUsingBinaryHeapNullArray() { findNthMaxNumberUsingBinaryHeap(null, 1); }
@Test(expected = IllegalArgumentException.class) public void testFindNthMaxNumberUsingBinaryHeapLowN() { findNthMaxNumberUsingBinaryHeap(new int[]{2, 1, 3, 4, 0}, 0); }
@Test(expected = IllegalArgumentException.class) public void testFindNthMaxNumberUsingBinaryHeapHighN() { findNthMaxNumberUsingBinaryHeap(new int[]{2, 1, 3, 4, 0}, 6); }
@Test public void testFindNthMaxNumberUsingBinaryHeap1() { int[] sequence = new int[]{2, 1, 3, 4, 0, -1, 7, 5}; assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 1), is(7)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 2), is(5)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 3), is(4)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 4), is(3)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 5), is(2)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 6), is(1)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 7), is(0)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 8), is(-1)); }
@Test public void testFindNthMaxNumberUsingBinaryHeap2() { int[] sequence = new int[]{2, -4, 5, 6, 0, 7, -1, 10, 9}; assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 1), is(10)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 2), is(9)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 3), is(7)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 4), is(6)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 5), is(5)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 6), is(2)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 7), is(0)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 8), is(-1)); assertThat(findNthMaxNumberUsingBinaryHeap(sequence, 9), is(-4)); }