forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxMirror.java
More file actions
51 lines (43 loc) · 1.62 KB
/
maxMirror.java
File metadata and controls
51 lines (43 loc) · 1.62 KB
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
/* We'll say that a "mirror" section in an array is a group of contiguous
* elements such that somewhere in the array, the same group appears in
* reverse order. For example, the largest mirror section in
* {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size
* of the largest mirror section found in the given array.
*/
public int maxMirror(int[] nums) {
int max = 0;
for(int i = 0; i < nums.length; i++) {
int count = 0;
for(int j = nums.length - 1; j >= 0 && i + count < nums.length; j--) {
if(nums[i + count] == nums[j]) {
count++;
} else {
max = Math.max(max, count);
count = 0;
}
}
max = Math.max(max, count);
}
return max;
}
// I think this version is easier to understand but it has 3 nested loops
// instead of just 2.
/*
public int maxMirror(int[] nums) {
int max = 0;
for(int start = 0; start < nums.length; start++) {
for(int begin = nums.length - 1; begin >= 0; begin--) {
int size = 0;
int i = start;
int j = begin;
while(i < nums.length && j >= 0 && nums[i] == nums[j]) {
size++;
i++;
j--;
}
max = Math.max(max, size);
}
}
return max;
}
*/