forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSpan.java
More file actions
22 lines (17 loc) · 708 Bytes
/
maxSpan.java
File metadata and controls
22 lines (17 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Consider the leftmost and righmost appearances of some value in an array.
* We'll say that the "span" is the number of elements between the two
* inclusive. A single value has a span of 1. Returns the largest span found
* in the given array. (Efficiency is not a priority.)
*/
public int maxSpan(int[] nums) {
int max = 0;
for(int i = 0; i < nums.length; i++) {
int j = nums.length - 1;
while(nums[i] != nums[j])
j--;
int span = j - i + 1;
if(span > max)
max = span;
}
return max;
}