-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestPositiveMissingNumber.java
More file actions
37 lines (35 loc) · 1.1 KB
/
SmallestPositiveMissingNumber.java
File metadata and controls
37 lines (35 loc) · 1.1 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
package Arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Vishal Singh */
public class SmallestPositiveMissingNumber {
static int smallestPositiveNumber(int[] arr,int n){
int[] temp = new int[n+1];
for (int i = 0; i < n; i++) {
if (arr[i]>=0 && arr[i]<=n){
temp[arr[i]] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (temp[i] == 0){
return i;
}
}
return n+1;
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
while (testCase-->0) {
int n = Integer.parseInt(br.readLine());
String[] input = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
System.out.println(smallestPositiveNumber(arr,n));
}
}
}