-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindOutlier.java
30 lines (27 loc) · 1.04 KB
/
FindOutlier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// You are given an array (which will have a length of at least 3, but could be very large) containing integers.
// The array is either entirely comprised of odd integers or
// entirely comprised of even integers except for a single integer N.
// Write a method that takes the array as an argument and returns this "outlier" N.
import java.lang.Math;
public class FindOutlier{
static int find(int[] integers){
if(Math.abs(integers[0]) % 2 == 0 && Math.abs(integers[1]) % 2 == 0 ){
for(int i = 0; i < integers.length;i++){
if(Math.abs(integers[i]) % 2 == 1){
return integers[i];
}
}
}
if(Math.abs(integers[0]) % 2 == 1 && Math.abs(integers[1]) % 2 == 1 ){
for(int i = 0; i < integers.length;i++){
if(Math.abs(integers[i]) % 2 == 0){
return integers[i];
}
}
}
if((Math.abs(integers[1]) % 2 == 0 && Math.abs(integers[2]) % 2 == 1) ||
(Math.abs(integers[1]) % 2 == 1 && Math.abs(integers[2]) % 2 == 0)){
return integers[1];
} else {
return integers[0];
}