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
guddu1cse1 day ago
time O(N) and space O(1)
 
static int missingNumber(int nums[], int size)   {       // Your code here       boolean one = false;       for(int y=0 ; y<nums.length ; y++){           if(nums[y]==1) one=true;                      if(nums[y]<1 || nums[y]>nums.length ) nums[y]=1;       }              if(!one) return 1;       for(int y=0 ; y<nums.length ; y++){           int idx= Math.abs(nums[y]);           nums[idx-1] = -Math.abs(nums[idx-1]);       }              for(int y=0 ; y<nums.length ; y++)           if(nums[y]>0) return y+1;       return  nums.length+1 ;   }
0
ar08052 days ago
+1
chicle0074 days ago
this like the easiest solution to this problem:
sort(arr, arr+n);       int minElement = 1;       for(int i=0; i<n;i++){           if(arr[i]==minElement)               minElement++;       }       return minElement;
0
raghavendra7774 days ago
Python 0.9 S
g=set(arr)       arr=list(g)       arr.sort()       if len(arr)==1 and arr[0]==0:           return max(arr)+1       if len(arr)==1 and 1 not in arr:           return 1       if 1 not in arr:           return 1       #print(arr)       #arr.sort()       k=arr.index(1)       s=arr[k:]       for i in range(0,len(s)):           #print(i+1,s[i])           if i+1!=s[i]:               return i+1       return max(arr)+1
0
aniket6518gadhe4 days ago
JAVA SOLUTION
Total Time Taken:
3.12/6.92
 
static int missingNumber(int arr[], int size)   {       if(size==1){           if(arr[0]==1){               return 2;           }else               return 1;       }       HashSet<Integer> map=new HashSet<>();       int missing_num=1;       for(int i=0;i<size;i++){           if(arr[i]>0)               map.add(arr[i]);       }       while(true){           if(map.contains(missing_num))               missing_num++;           else               break;       }       return missing_num;   }
0
avinav26115 days ago
Easy C++ Solution ( 1.18 / 2.2 sec) in O(1) space-complexity
 
+1
kartikeyashokgautam1 week ago
Simple JAVA Solution :- 
 static int missingNumber(int arr[], int size)   {       int[] brr = new int[1000000];     for(int i=0;i<size;i++)   {         if(arr[i]>0)         {             brr[arr[i]] =1;         }        }    for(int i=1;i<1000000;i++)    {             if(brr[i]==0)     {         return i;     }         }       return 0;          }
0
saicharanthammi1 week ago
int missingNumber(int arr[], int n)    {        unordered_map<int,int> mp;       for(int i=0;i<n;i++)            mp[arr[i]]++;      for(int i=1;i<=n+1;i++)         if(mp[i]==0)           return i;   } 
0
aman hamid1 week ago
O(1) space solution:
 
Logic: If size is n and all numbers are present then if we take % with n then all possible index in the array will be generated.
 
As we have to find smallest missing positive number therefore we can make all negative numbers, zeros and numbers>n as 1 (Logic : Because the largest possible missing number can be n+1 any number above that is irrelevant to us)
 
Now run a loop for the above logic and also keep a check for 1, if 1 is not present then 1 is the missing number.
 
Now go over to every index and take arr[i]%n to generate an index (this basically ensures that this particular number is present in the array) and increment the value at index by n.
 
After iterating through the array check where value is less than n, that is the answer.
 
class Solution{   public:   //Function to find the smallest positive number missing from the array.   int missingNumber(int arr[], int n)    {        // Your code here       int i;       int flag = 0;       for(int i=0;i<n;i++) {           if(arr[i]==1) {               flag = 1;           }           if(arr[i]<=0 || arr[i]>n) {               arr[i]=1;           }       }       if(!flag) {           return 1;       }       for(i=0;i<n;i++) {           int index = (arr[i]%n);           arr[index]+= n;           //cout<<arr[index]<<" "<<index<<endl;       }       int minEle = INT_MAX;       for(i=0;i<n;i++) {           //cout<<arr[i]<<endl;           if(arr[i]<n && i==0) {               if(minEle>n) {                   minEle = n;               }           } else if(i!=0 && arr[i]<n) {               if(minEle>i) {                   minEle = i;               }           }       }       //cout<<"YES"<<endl;       minEle = min(minEle,n+1);       return minEle;          } };
0
manish752902 weeks ago
//Java simplest code using HashSet

static int missingNumber(int arr[], int size)
    {
        // Your code here
        HashSet<Integer> set = new HashSet<>();
        for(int i=0; i<size; i++){
            set.add(arr[i]);
        }
        int maxElement = Collections.max(set);
        for(int i=1; i<=maxElement; i++){
            if(!set.contains(i)){
                return i;
            }
        }
        return maxElement+1;
    }
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.