Concise Code With comments


#1
int Solution::firstMissingPositive(vector<int> &nums) {
          // ignore any numbers less tah 0 or greater tahn n, because ans lies in [1,n+1]
        //lets mark those numbers something -- lets say n+1 bcz -1 se multiply karna hoga baad me
        //
        int n = nums.size();
        for(int i = 0; i < n; i++){
            if(nums[i] <= 0 || nums[i] > n) nums[i] = n+1;
        }
        
        for(int i = 0; i < n; i++){
            int curr = abs(nums[i]);
            if(curr > n) continue;
            
            if(nums[curr-1] > 0){ //to handle duplicates - preventing making negatives positive again
                nums[curr-1] *= -1;
            } 
        }
        
        for(int i = 0; i < n; i++){
            if(nums[i] > 0) return i+1; 
        }
        
        return n+1;
}