Search in sources :

Example 1 with IntPair

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;
}
Also used : IntPair(com.shekhargulati.IntPair)

Example 2 with IntPair

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());
}
Also used : OptionalInt(java.util.OptionalInt) IntPair(com.shekhargulati.IntPair)

Example 3 with IntPair

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)));
}
Also used : IntPair(com.shekhargulati.IntPair) Test(org.junit.Test)

Example 4 with IntPair

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)));
}
Also used : IntPair(com.shekhargulati.IntPair) Test(org.junit.Test)

Example 5 with IntPair

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)));
}
Also used : IntPair(com.shekhargulati.IntPair) Test(org.junit.Test)

Aggregations

IntPair (com.shekhargulati.IntPair)6 Test (org.junit.Test)4 OptionalInt (java.util.OptionalInt)1