From 9b131cac50b3e477ff7e6ffe55722c29c0cf23c4 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 21 Oct 2023 17:58:25 +0530 Subject: [PATCH] Add solution for First Missing Positive in Hashing --- 21- Hashing/FirstMissingPositive.cpp | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 21- Hashing/FirstMissingPositive.cpp diff --git a/21- Hashing/FirstMissingPositive.cpp b/21- Hashing/FirstMissingPositive.cpp new file mode 100644 index 0000000..30673d3 --- /dev/null +++ b/21- Hashing/FirstMissingPositive.cpp @@ -0,0 +1,80 @@ +/* + +First Missing Positive (link - https://leetcode.com/problems/first-missing-positive/description/) + +Description + +Given an unsorted integer array nums, return the smallest missing positive integer. + +You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. + + + +Example 1: + +Input: nums = [1,2,0] +Output: 3 +Explanation: The numbers in the range [1,2] are all in the array. + +Example 2: + +Input: nums = [3,4,-1,1] +Output: 2 +Explanation: 1 is in the array but 2 is missing. + +Example 3: + +Input: nums = [7,8,9,11,12] +Output: 1 +Explanation: The smallest positive integer 1 is missing. + +Constraints: + +1 <= nums.length <= 105 +-231 <= nums[i] <= 231 - 1 + + + +*/ + + +#include +using namespace std; + +// Function to find first positive integer missing from the given list +int firstMissingPositive(vector& nums) { + int n = nums.size(); + for(int i=0;i= 1 && nums[i] != nums[nums[i]-1]) swap(nums[i], nums[nums[i]-1]),i--; + } + } + + // Check which location is not containing its respective integer + for(int i=0;i>n; + cout<<"Enter Numbers:\n"; + vector nums; + for(int i=0;i>t; + nums.push_back(t); + } + + cout<<"First Missing Positive: "<