Skip to content

Commit

Permalink
41. First Missing Positive
Browse files Browse the repository at this point in the history
  • Loading branch information
chhavibansal committed Oct 22, 2024
1 parent cd335dc commit 2c02f96
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions LeetcodeProblems/Algorithms/hard/First_Missing_Positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ If all elements are in their correct positions, the function returns the next po
* @return {number}
*/
var firstMissingPositive = function(nums) {
const n = nums.length
const n = nums.length;

for (let i = 0; i < n; i++)
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] !== nums[i])
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]];

for (let i = 0; i < n; i++)
if (nums[i] !== i + 1)
return i + 1
return i + 1;

return n + 1
return n + 1;
};

module.exports.firstMissingPositive = firstMissingPositive;

0 comments on commit 2c02f96

Please sign in to comment.