From 5e20bda7f20671d4e1194cc2a261191da2b5b2d1 Mon Sep 17 00:00:00 2001 From: Shivangi Date: Mon, 30 Dec 2024 20:52:14 +0530 Subject: [PATCH] feat(positive): first missing positive cpp --- C++/first-missing-positive.cpp | 40 ++++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 C++/first-missing-positive.cpp diff --git a/C++/first-missing-positive.cpp b/C++/first-missing-positive.cpp new file mode 100644 index 00000000..46074451 --- /dev/null +++ b/C++/first-missing-positive.cpp @@ -0,0 +1,40 @@ +/* +Problem - + 41. First Missing Positive + Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums. You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. + +The Optimized Solution - + Time Complexity: O(N) + Space Complexity: O(1) + +Example - + Input: nums = [1,2,0] + Output: 3 + Explanation: The numbers in the range [1,2] are all in the array. + + Input: nums = [3,4,-1,1] + Output: 2 + Explanation: 1 is in the array but 2 is missing. +*/ + + +class Solution { +public: + int firstMissingPositive(vector& nums) { + int n = nums.size(); + + for (int i = 0; i < n; i++) { + while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) { + swap(nums[i], nums[nums[i] - 1]); + } + } + + for (int i = 0; i < n; i++) { + if (i + 1 != nums[i]) { + return i + 1; + } + } + + return n + 1; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index b2cac739..5232b160 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Java](./Java/can-make-arithmetic-progression-from-sequence.java) | _O(n)_ | _O(1)_ | Easy | Array | | | 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py)
[C++](./C++/Best-Time-to-Buy-and-Sell-Stock-II.cpp) | _O(N)_ | _O(1)_ | Medium | Stocks | | | 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | | -| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | | +| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | +| 41 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | [C++](./C++/first-missing-positive.cpp) | _O(N)_ | _O(1)_ | Hard | Array | | | 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./Python/trapping_rain.py) | _O(N^2)_ | _O(N)_ | Hard | Array | | | 11 | [Container with Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](./Python/container_with_most_water.py) | _O(N)_ | _O(N)_ | Medium | Array Two Pointers | | | 1134 🔒 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Java](./Java/Armstrong-Number.java) | _O(N)_ | _O(1)_ | Easy | | |