Smallest Positive missing number | Practice | GeeksforGeeks

You are given an array arr[] of N integers including 0. The task is to find the smallest positive number missing from the array.
Example 1:
Input:
N = 5
arr[] = {1,2,3,4,5}
Output: 6
Explanation: Smallest positive missing 
number is 6.

Example 2:
Input:
N = 5
arr[] = {0,-10,1,3,-20}
Output: 2
Explanation: Smallest positive missing 
number is 2.
Your Task:
The task is to complete the function missingNumber() which returns the smallest positive missing number in the array.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 106
-106 <= arr[i] <= 106
 
0
abuzart199913 hours ago
Easy Java Solution:
 
int miss = 1;       Arrays.sort(arr);       for(int i = 0;i<size;i++){           if(arr[i]>0&&miss==arr[i])               miss++;       }       return miss;
0
bharathk1n7t1 day ago
int missingNumber(int[] array, int n)       {                      var vs = array.Where(x => x > 0).OrderBy(x => x).AsEnumerable();           var result = 0;           if (vs.Any(x => x == 1))           {               for (int j = 0; j <= vs.Count() - 1; j++)               {                   if (vs.Any(x => x == result) || j == 0)                   {                       result = vs.ElementAt(j) + 1;                   }
               }           }           else           {               return 1;           }           return result;       }
+5
kaganesh122 days ago
easiest way to do this is as follows:
 
int missingNumber(int arr[], int n)    {              sort(arr,arr+n);       int count=1;       for (int i=0;i<n;i++)       {           if (arr[i]==count)           count++;       }       return count;   } 
-1
hrishabhgarg0074 days ago
class Solution{   //Function to find the smallest positive number missing from the array.   static int missingNumber(int arr[], int size)   {       Arrays.sort(arr);       int i=0,res=1;       while(i<size){           if(arr[i]<res){               i++;           }           else if(arr[i]==res){               res++;           }           else{               return res;           }       }       return(res);}}
-2
akyadav133984 days ago
 int missingNumber(int a[], int n)    {            int i;      int j=1;  sort(a,a+n);      for(i=0;i<n;i++)      {  if(a[i]<=0)continue;                    if(j==a[i])          {j++;         }                }           return j;                    } 
+1
shivamsingh001414 days ago
class Solution
{
    public:
    //Function to find the smallest positive number missing from the array.
    int missingNumber(int arr[], int n) 
    { 
        for(int i = 0; i < n; i++) {
            while(arr[i] > 0 && arr[i] <= n && arr[i] != arr[arr[i] - 1]) {
                swap(arr[i], arr[arr[i] - 1]);
            }
        }
        
        for(int i = 0; i < n; i++) {
            if(arr[i] != i + 1) return i + 1;
        }
        
        return n + 1;
    } 
};
0
ruchikajamwal03195 days ago
class Solution{   public:   //Function to find the smallest positive number missing from the array.   int missingNumber(int arr[], int n)    {        // Your code here       int temp=0;       for(int i=0;i<n;i++){           if(arr[i]>=1 && arr[i]<=n && arr[i]!=arr[arr[i]-1]){               swap(arr[i],arr[arr[i]-1]);               i--;           }   }           for(int i=0;i<n;i++){           if(arr[i]!=i+1){               return i+1;           }       }              return n+1;   }}; 
0
sahiltushamar6 days ago
Can Anyone Correct the mistake, Logical Error Segmentation Fault.
int missingNumber(int arr[], int n)    {        int i=1;       int N = INT_MAX;       bool check[N];       for(i=1;i<=N;i++){           check[i]=false;       }       for(i=1;i<=n;i++){           if(arr[i]>=1){               check[arr[i]]=true;           }       }       int count=-1;       for(i=1;i<=N;i++){           if(check[i]==true){               count++ ;           }       }       return count;   } 
 
0
aks766251 week ago
int missingNumber(int arr[], int n) 
    { 
        int temp=0;
        for(int i=0;i<n;i++){
            if(arr[i]>=1 && arr[i]<=n && arr[i]!=arr[arr[i]-1]){
                swap(arr[i],arr[arr[i]-1]);
                i--;
            }
        }
        
        for(int i=0;i<n;i++){
            if(arr[i]!=i+1){
                return i+1;
            }
        }
        
        return n+1;
    } 
-1
jubin kumar soni1 week ago
int missingNumber(int arr[], int n) 
    { 
        // Your code here
        unordered_set<int> us;
        int res = 1;
        for(int i=0;i<n;i++)
        {
            if(arr[i] == res)
            {
                res = res + 1;
                while(us.find(res) != us.end())
                {
                    res = res + 1;
                }
            }
            else if(arr[i] > 0)
            {
                us.insert(arr[i]);
            }
            
            /*
            if(arr[i] > res)
            {
                // pass
            }
            */
        }
        
        return res;
    } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.