-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem no. 41.java
38 lines (31 loc) · 993 Bytes
/
Problem no. 41.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
// Ques : First missing positive
// Ques Link : https://leetcode.com/problems/first-missing-positive/
package com.nitin;
public class FirstMissingPositive {
public static void main(String[] args) {
int[] arr = {3, 4, -1, 1};
System.out.println(firstMissingPositive(arr));
}
public static int firstMissingPositive(int[] arr) {
int i = 0;
while ( i < arr.length) {
int correct = arr[i] - 1;
if (arr[i] > 0 && arr[i] <= arr.length && arr[correct] != arr[i]) {
swap(arr, i, correct);
} else {
i++;
}
}
for (int index = 0; index < arr.length; index++) {
if (arr[index] != index + 1) {
return index + 1;
}
}
return arr.length + 1;
}
static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}