Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1866 from itsrishub/main
Browse files Browse the repository at this point in the history
Create 0041-first-missing-positive.java
  • Loading branch information
tahsintunan authored Jan 3, 2023
2 parents 37e5cff + c9e7656 commit 303cb40
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions java/0041-first-missing-positive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length, size = 0;

while(n > 0){
n = n>>1;
size++;
}
n = nums.length;
int pivot = 0;


for(int i = 0; i < n; i++){
if(nums[i] <= 0 || nums[i] > n){
int temp = nums[i];
nums[i] = nums[pivot];
nums[pivot] = temp;
pivot++;
}
}
for(int i= 0; i < pivot; i++)
nums[i] = 0;
for(int i= pivot; i < n; i++){
nums[(nums[i] - 1)&((1<<size) - 1)] |= (1<<size);
}
for(int i= 0; i < n; i++)
if((nums[i] & (1<<size)) == 0)
return i+1;
return n+1;
}
}

0 comments on commit 303cb40

Please sign in to comment.