First Missing Positive LeetCode solution in Java
1class Solution {2public int firstMissingPositive(int[] nums) {3// cyclic sort4int i = 0;5while (i < nums.length) {6int correct = nums[i] - 1;7if (nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[correct]) {8swap(nums, i, correct);9} else {10i++;11}12}13// linear search to find the missing number14for (int index = 0; index < nums.length; index++) {15if (nums[index] != index + 1) {16return index + 1;17}18}19// if array has all the elements match to its index then 1st missing num will be20// nums.length+121return nums.length + 1;22}2324static void swap(int[] arr, int a, int b) {25int temp = arr[a];26arr[a] = arr[b];27arr[b] = temp;28}29}