{"id":22513843,"url":"https://github.com/emahtab/first-missing-positive","last_synced_at":"2026-01-06T18:09:07.918Z","repository":{"id":79525500,"uuid":"275523232","full_name":"eMahtab/first-missing-positive","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2020-07-31T03:07:00.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:35.355Z","etag":null,"topics":["constant-space","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-28T06:36:16.000Z","updated_at":"2020-07-31T03:07:02.000Z","dependencies_parsed_at":"2023-05-10T17:15:53.296Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/first-missing-positive","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffirst-missing-positive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffirst-missing-positive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffirst-missing-positive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffirst-missing-positive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/first-missing-positive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952058,"owners_count":20699423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constant-space","leetcode","problem-solving"],"created_at":"2024-12-07T03:14:38.406Z","updated_at":"2026-01-06T18:09:07.891Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# First Missing Positive\n## https://leetcode.com/problems/first-missing-positive\n\nGiven an unsorted integer array, find the smallest missing positive integer.\n```\nExample 1:\n\nInput: [1,2,0]\nOutput: 3\n\nExample 2:\n\nInput: [3,4,-1,1]\nOutput: 2\n\nExample 3:\n\nInput: [7,8,9,11,12]\nOutput: 1\n```\n**Note: Your algorithm should run in O(n) time and uses constant extra space.**\n\n# Implementation 1 : O(n) Space\n```java\nclass Solution {\n    public int firstMissingPositive(int[] nums) {\n        Set\u003cInteger\u003e set = new HashSet\u003cInteger\u003e();\n        for(int num : nums)\n            set.add(num);\n        int i = 1;\n        for(; i \u003c= nums.length; i++) {\n            if(!set.contains(i))\n                 return i;\n        }\n       return i; \n    }\n}\n```\n\n# Implementation 2 : O(1) Constant Space\n```java\nclass Solution {\n    public int firstMissingPositive(int[] nums) {\n       if(nums == null || nums.length == 0)\n           return 1;\n        int n = nums.length;\n        boolean containsOne = false;\n        \n        // Step 1 : Sets the containsOne flag to true if input array contains 1 ,\n        // also sets all the negative numbers, zero and number's greater than n, to 1 \n        // Make sure you don't mess up the order of statements\n        for(int i = 0; i \u003c n; i++) {\n            if(nums[i] == 1) \n                containsOne = true; \n            else if(nums[i] \u003c= 0 || nums[i] \u003e n)\n                nums[i] = 1;\n        }\n        \n        // if array doesn't contains 1, the first missing positive will be 1\n        if(!containsOne) return 1;\n        \n        // Step 2 : This step marks the number we have seen, by setting its index \n        // position to a negative number \n        // e.g. if nums[i] = 7, since 7 should come at index 6 (that's why - 1, array indexes starts with 0)\n        // we mark nums[6] = nums[6] * -1 (if nums[6] was not already negative)\n        for(int i = 0; i \u003c n; i++) {\n            int index = Math.abs(nums[i]) - 1;\n            // if its already negative, we don't do anything\n            if(nums[index] \u003e 0)\n                nums[index] *= -1;\n        }\n        \n        // Step 3 : Finally, look for a non negative number in the array\n        // If we find an index i, such that nums[i] is positive, that means\n        // we didn't see the number which should come at this index i\n        // the number that should come at index i should be i+1, so we return i+1\n        for(int i = 0; i \u003c n; i++) {\n            if(nums[i] \u003e 0) return i + 1;\n        }  \n        \n        // If we reach here it means, original input array had all the positive numbers \n        // from 1 to n, so the first missing positive number will be n + 1\n        return n + 1;\n    }\n}\n```\n\n### Tip :\nRun your code through some examples :\n```\n[1, 2, 3],\n[1, 2, 2],\n[1, 3, 3],\n[2, 2, 2],\n[4, 5, 1]\n```\n# References :\nhttps://www.youtube.com/watch?v=9SnkdYXNIzM\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffirst-missing-positive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Ffirst-missing-positive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffirst-missing-positive/lists"}