Created
November 30, 2018 22:27
-
-
Save avraamisvi/a8018907593a81f852b8221382f3cb77 to your computer and use it in GitHub Desktop.
Find The Parity Outlier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.Arrays; | |
import java.util.function.IntPredicate; | |
public class FindOutlier{ | |
enum Filter { | |
EVEN((i) -> mod(i) == 0), ODD((i) -> mod(i) > 0); | |
IntPredicate validator; | |
Filter(IntPredicate validator) { | |
this.validator = validator; | |
} | |
boolean contains(int number) { | |
return validator.test(number); | |
} | |
static Filter using(int[] integers) { | |
int arr[] = { | |
integers[0], | |
integers[integers.length / 2], | |
integers[integers.length - 1]}; | |
int evens = 0; | |
for (int i : arr) { | |
evens += EVEN.contains(i) ? 1 : 0; | |
} | |
return evens >= 2 ? ODD : EVEN; | |
} | |
} | |
static int mod(final int i) { | |
return Math.abs(i%2); | |
} | |
static int find(int[] integers) { | |
return Arrays.stream(integers) | |
.filter(filter(Filter.using(integers))) | |
.findFirst() | |
.orElse(integers[0]); | |
} | |
private static IntPredicate filter(Filter using) { | |
return using::contains; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My aim was to create a solution with a low time complexity but also with a good design that would be easy to understand.