-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMirror.java
More file actions
44 lines (34 loc) · 1.08 KB
/
MaxMirror.java
File metadata and controls
44 lines (34 loc) · 1.08 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
/*
* 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.
maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3
maxMirror([1, 2, 1, 4]) → 3
maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2
*/
package array3;
public class MaxMirror {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = { 1, 2, 3, 8, 9, 3, 2, 1 };
System.out.println(maxMirror(nums));
}
public static int maxMirror(int[] nums) {
int max = 0;
int count = 0;
for(int i = 0; i < nums.length; i ++) {
for(int j = nums.length-1; j >= 0 ; j --) {
int tmp =i;
int tmp2 = j;
while( tmp < nums.length && tmp2 >= 0 && nums[tmp] ==nums[tmp2]) {
tmp++;
tmp2--;
count++;
}
if(count> max) {
max = count;
}
count = 0;
}
}
return max;
}
}