use of com.shekhargulati.IntPair in project 99-problems by shekhargulati.
the class Problem4_2 method minimize_sorted.
public static IntPair minimize_sorted(int[] sorted) {
int diff = sorted[1] - sorted[0];
IntPair pair = null;
for (int i = 1; i < sorted.length - 1; i++) {
int first = sorted[i];
int second = sorted[i + 1];
if (diff > (second - first)) {
diff = second - first;
pair = new IntPair(first, second);
}
}
return pair;
}
use of com.shekhargulati.IntPair in project 99-problems by shekhargulati.
the class Problem4_2 method maximize_unsorted.
public static IntPair maximize_unsorted(int[] numbers) {
OptionalInt min = IntStream.of(numbers).min();
OptionalInt max = IntStream.of(numbers).max();
return new IntPair(min.getAsInt(), max.getAsInt());
}
use of com.shekhargulati.IntPair in project 99-problems by shekhargulati.
the class Problem4_2Test method shouldFindPairThatMaximizeWithinSortedArray.
@Test
public void shouldFindPairThatMaximizeWithinSortedArray() throws Exception {
int[] numbers = { 3, 6, 8, 13, 19 };
IntPair p = Problem4_2.maximize_sorted(numbers);
assertThat(p, equalTo(new IntPair(3, 19)));
}
use of com.shekhargulati.IntPair in project 99-problems by shekhargulati.
the class Problem4_2Test method shouldFindPairThatMinimiseWithinSortedArray.
@Test
public void shouldFindPairThatMinimiseWithinSortedArray() throws Exception {
int[] numbers = { 3, 6, 8, 13, 19 };
IntPair p = Problem4_2.minimize_sorted(numbers);
assertThat(p, equalTo(new IntPair(6, 8)));
}
use of com.shekhargulati.IntPair in project 99-problems by shekhargulati.
the class Problem4_2Test method shouldFindPairThatMinimizeWithinUnsortedArray.
@Test
public void shouldFindPairThatMinimizeWithinUnsortedArray() throws Exception {
int[] numbers = { 6, 13, 19, 3, 8, 14 };
IntPair p = Problem4_2.minimize_unsorted(numbers);
assertThat(p, equalTo(new IntPair(13, 14)));
}
Aggregations