`{"id":"e699c1c8-0883-449f-96dd-3de485cbfe1d","name":"First Missing Positive","description":"1. Given an unsorted integer array 'arr'.\r\n2. Find the smallest missing positive integer.\r\nNote : You must implement an algorithm that runs in O(n) time and uses constant extra space.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.","constraints":"1. 1 &lt;= nums.length &lt;= 10^5\r\n2. -2^31 &lt;= nums[i] &lt;= 2^31 - 1\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint firstMissingPositive(vector<int>& arr){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int res = firstMissingPositive(arr);\n cout<<res<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\r\n //~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~~~~\r\n public static int firstMissingPositive(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n //~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int res = firstMissingPositive(arr);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"4\r\n3 4 -1 1","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"86e43c89-024d-4e90-97b4-c58ebfcf0ce7","name":"First Missing Positive","slug":"first-missing-positive","type":1}],"next":{"id":"a181dc16-7313-4fdf-9bc1-e333c4669c76","name":"First Missing Positive","type":3,"slug":"first-missing-positive"},"prev":{"id":"95583ade-75ff-4541-a4b6-a061b14b622f","name":"Maximum Swap MCQ","type":0,"slug":"maximum-swap-mcq"}}}`

# First Missing Positive

1. Given an unsorted integer array 'arr'. 2. Find the smallest missing positive integer. Note : You must implement an algorithm that runs in O(n) time and uses constant extra space.

`{"id":"e699c1c8-0883-449f-96dd-3de485cbfe1d","name":"First Missing Positive","description":"1. Given an unsorted integer array 'arr'.\r\n2. Find the smallest missing positive integer.\r\nNote : You must implement an algorithm that runs in O(n) time and uses constant extra space.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.","constraints":"1. 1 &lt;= nums.length &lt;= 10^5\r\n2. -2^31 &lt;= nums[i] &lt;= 2^31 - 1\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint firstMissingPositive(vector<int>& arr){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int res = firstMissingPositive(arr);\n cout<<res<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\r\n //~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~~~~\r\n public static int firstMissingPositive(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n //~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int res = firstMissingPositive(arr);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"4\r\n3 4 -1 1","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"86e43c89-024d-4e90-97b4-c58ebfcf0ce7","name":"First Missing Positive","slug":"first-missing-positive","type":1}],"next":{"id":"a181dc16-7313-4fdf-9bc1-e333c4669c76","name":"First Missing Positive","type":3,"slug":"first-missing-positive"},"prev":{"id":"95583ade-75ff-4541-a4b6-a061b14b622f","name":"Maximum Swap MCQ","type":0,"slug":"maximum-swap-mcq"}}}`

Editor

# First Missing Positive

hard

1. Given an unsorted integer array 'arr'. 2. Find the smallest missing positive integer. Note : You must implement an algorithm that runs in O(n) time and uses constant extra space.

## Constraints

1. 1 <= nums.length <= 10^5 2. -2^31 <= nums[i] <= 2^31 - 1

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 3 4 -1 1```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2`

Discussions

Show Discussion

Related Resources