-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallest_number.java
More file actions
33 lines (31 loc) · 940 Bytes
/
Smallest_number.java
File metadata and controls
33 lines (31 loc) · 940 Bytes
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
import java.util.*;
import java.io.*;
public class Smallest_number
{
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
int k = s.nextInt();
Arrays.sort(arr);
//System.out.println(Arrays.toString(arr));
System.out.println(smallest(size, arr, k));
}
//Optimal Approach
private static int smallest(int size, int[] arr, int min) {
HashSet<Integer> set = new HashSet<>();
int count = 1;
for(int i = 0; i < size; i++){
if(set.contains(arr[i])){
count++;
}if(count == min){
return arr[i];
}
set.add(arr[i]);
}
return -1;
}
}