CoupleSumFinder { @Complexity("O(n . lg(n))") public Couple findCouple(int sum, int... target) { Arrays.sort(target); int i = 0; int j = target.length - 1; while (i >= 0 && j < target.length && i < j) { final int result = target[i] + target[j]; if (result == sum) { return new Couple(target[i], target[j]); } else if (result < sum) { i ++; } else { j --; } } return null; }  @Complexity("O(n . lg(n))") Couple findCouple(int sum, int... target);  }
@Test public void testLookingForNonExistentSum() throws Exception { final CoupleSumFinder finder = new CoupleSumFinder(); assertThat(finder.findCouple(0, 1, 2, 3, 4, 5, 6), is(nullValue())); assertThat(finder.findCouple(20, 1, 2, 3, 4, 5, 6), is(nullValue())); assertThat(finder.findCouple(Integer.MIN_VALUE, 1, 2, 3, 4, 5, 6), is(nullValue())); assertThat(finder.findCouple(Integer.MAX_VALUE, 1, 2, 3, 4, 5, 6), is(nullValue())); }
@Test public void testLookingForNegativeSum() throws Exception { final CoupleSumFinder finder = new CoupleSumFinder(); assertThat(finder.findCouple(-20, -30, 2, 3, 4, 10, 5), is(new Couple(-30, 10))); assertThat(finder.findCouple(-30, 5, 6, 7, -36, -37), is(new Couple(7, -37))); }
@Test public void testLookingForPositiveSum() throws Exception { final CoupleSumFinder finder = new CoupleSumFinder(); assertThat(finder.findCouple(20, 5, 10, 15, 20), is(new Couple(5, 15))); assertThat(finder.findCouple(20, 5, 7, 10, 15, 20, 13), is(new Couple(5, 15))); }
@Test public void testLookingForZero() throws Exception { final CoupleSumFinder finder = new CoupleSumFinder(); assertThat(finder.findCouple(0, 5, 6, -5, -6, -7), is(new Couple(-6, 6))); }