-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsCentered.java
59 lines (48 loc) · 2.64 KB
/
IsCentered.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class IsCentered {
// An array with an odd number of elements is said to be centered
// if all elements (except the middle one) are strictly greater than the value
// of the middle element. Note that only arrays with an odd number of elements
// have a middle element. Write a function that accepts an integer array and
// returns 1 if it is a centered array, otherwise it returns 0.
// Examples:
// -----------------------|-----------------------------------------------------------------------
// | if the input array is | return |
// |-----------------------|-----------------------------------------------------------------------|
// | {1,2,3,4,5} | 0 (the middle element 3 is not strictly less than all other elements) |
// |-----------------------|-----------------------------------------------------------------------|
// | {3,2,1,4,5} | 1 (the middle element 1 is strictly less than all other elements) |
// |-----------------------|-----------------------------------------------------------------------|
// | {3,2,1,4,1} | 0 (the middle element 1 is not strictly less than all other elements) |
// |-----------------------|-----------------------------------------------------------------------|
// | {1,2,3,4} | 0 (no middle element) |
// |-----------------------|-----------------------------------------------------------------------|
// | {} | 0 (no middle element) |
// |-----------------------|-----------------------------------------------------------------------|
// | {10} | 1 (the middle element 10 is strictly less than all other elements) |
// -----------------------------------------------------------------------------------------------
static int isCentered(int [] items) {
if (items.length == 0 || items.length % 2 == 0) {
return 0;
}
int midIndex = items.length / 2;
int midItem = items[midIndex];
for (int i = 0; i < items.length ; i++) {
if (i != midIndex && midItem >= items[i]) {
return 0;
}
}
return 1;
}
static void isCenteredTest() {
System.out.println(isCentered(new int[] {1,2,3,4,5}));
System.out.println(isCentered(new int[] {3,2,1,4,5}));
System.out.println(isCentered(new int[] {3,2,1,4,1}));
System.out.println(isCentered(new int[] {1,2,3,4}));
System.out.println(isCentered(new int[] {}));
System.out.println(isCentered(new int[] {10}));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
isCenteredTest();
}
}