-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAngryCows.java
More file actions
39 lines (37 loc) · 1.04 KB
/
AngryCows.java
File metadata and controls
39 lines (37 loc) · 1.04 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
import java.util.Scanner;
import java.util.Arrays;
public class AngryCows {
static int n;
static int[] bales;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
bales = new int[n];
int ans = 0;
for(int i = 0; i<n; i++){
bales[i] = sc.nextInt();
}
Arrays.sort(bales);
for(int i = 0; i<n; i++){
ans = Math.max(ans, explodedNum(i, -1) + explodedNum(i, 1) + 1);
}
System.out.println(ans);
sc.close();
}
public static int explodedNum(int start, int direction){
int radius = 1;
int prev = start;
while(true){
int next = prev;
while(next+direction>=0 && next+direction<n && Math.abs(bales[next+direction]-bales[prev])<=radius){
next+=direction;
}
if(next == prev){
break;
}
prev=next;
radius++;
}
return Math.abs(prev-start);
}
}