-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForestFire.java
More file actions
34 lines (25 loc) · 1.02 KB
/
ForestFire.java
File metadata and controls
34 lines (25 loc) · 1.02 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
package com.contest.techgig.y2023;
/* Read input from STDIN. Print your output to STDOUT*/
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class ForestFire {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(System.in);
String firstLine = s.nextLine();
String[] values = firstLine.split(" ");
int noOfAnimals = Integer.parseInt(values[0]);
int capacity = Integer.parseInt(values[1]);
String secondLine = s.nextLine();
List<Long> energies = Arrays.stream(secondLine.split(" ")).map(Long::parseLong).collect(Collectors.toList());
Collections.sort(energies, Collections.reverseOrder());
if (noOfAnimals != capacity && energies.get(capacity - 1).equals(energies.get(capacity))) {
System.out.println("-1");
} else {
System.out.println(energies.get(capacity - 1));
}
s.close();
}
}