-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrees.java
39 lines (31 loc) · 919 Bytes
/
Trees.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
// Success in 0.48s
import java.util.*;
public class Trees{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] trees = new int[n];
for(int i = 0; i < n; i++)
trees[i] = reader.nextInt();
reader.close();
// for(int i = 0; i < trees.length - 1; i++)
// for(int j = i; j < trees.length; j++)
// if(trees[j] >= trees[i]){
// int temp = trees[j];
// trees[j] = trees[i];
// trees[i] = temp;
// }
Arrays.sort(trees);
for(int i = 0; i < (trees.length) / 2; i++){
int temp = trees[i];
trees[i] = trees[trees.length - i - 1];
trees[trees.length - i -1] = temp;
}
int longest = 0;
for(int i = 0; i < trees.length; i++){
if((trees[i] + i) > longest)
longest = trees[i] + i;
}
System.out.println(longest + 2);
}
}