-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
42 lines (34 loc) · 1.04 KB
/
Copy pathProblem4.java
File metadata and controls
42 lines (34 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
40
41
42
/*Given an array of integers, find the first missing positive integer in linear time and constant space.
*In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
*For example, the input [3, 4, -1, 1] should give 2.
*The input [1, 2, 0] should give 3.
*You can modify the input array in-place.*/
import java.util.Scanner;
public class DAY4 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
int i;
for(i=0;i<n;i++)
{
int k=sc.nextInt();
if(k>0)
{
arr[k-1]=2;
}
}
for(i=0;i<n;i++)
{
if(arr[i]!=2)
{
System.out.println(i+1);
break;
}
}
if(i==n)
{
System.out.println(n+1);
}
}
}