Open In App

Smallest Missing Positive Number

Last Updated : 27 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an unsorted array arr[] with both positive and negative elements, find the smallest positive number missing from the array.

Examples:

Input: arr[] = [2, -3, 4, 1, 1, 7]
Output: 3
Explanation: 3 is the smallest positive number missing from the array.

Input: arr[] = [5, 3, 2, 5, 1]
Output: 4
Explanation: 4 is the smallest positive number missing from the array.

Input: arr[] = [-8, 0, -1, -4, -3]
Output: 1
Explanation: 1 is the smallest positive number missing from the array.

[Naive approach] By Sorting - O(n*log n) Time and O(1) Space

The idea is to sort the array and assume the missing number as 1. Now, iterate over the array and for each element arr[i],

  • If arr[i] == missing number, then increment missing number by 1.
  • If arr[i] < missing number, then continue to search for the missing number.
  • If arr[i] > missing number, then break and return the missing number
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int missingNumber(vector<int> &arr) {
    
    sort(arr.begin(), arr.end());
    int res = 1;
    for (int i = 0; i < arr.size(); i++) {

        // If we have found 'res' in the array,
        // 'res' is no longer missing, so increment it
        if (arr[i] == res) 
            res++;

        // If the current element is larger than 'res',
        // 'res' cannot be found in the array,
        // so it is our final answer
        else if (arr[i] > res) 
            break;
    }
    return res;
}

int main() {
    vector<int> arr = {2, -3, 4, 1, 1, 7};

    cout << missingNumber(arr);
    return 0;
}
C
#include <stdio.h>

int cmp(const int *a, const int *b) {
    return (*a - *b);
}

int missingNumber(int arr[], int size) {
	
	// sort the array
    qsort(arr, size, sizeof(int), (int(*)(const void*, const void*))cmp);

    // res will hold the current smallest missing number,
    // initially set to 1
    int res = 1;
    for (int i = 0; i < size; i++) {

        // If we have found 'res' in the array,
        // 'res' is no longer missing, so increment it
        if (arr[i] == res) {
            res++;
        }

        // If the current element is larger than 'res',
        // 'res' cannot be found in the array,
        // so it is our final answer
        else if (arr[i] > res) {
            break;
        }
    }
    return res;
}

int main() {
    int arr[] = {2, -3, 4, 1, 1, 7};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("%d", missingNumber(arr, size));
    return 0;
}
Java
import java.util.Arrays;
class GfG {
    
    static int missingNumber(int[] arr) {
        Arrays.sort(arr);

        // res will hold the current smallest missing number,
        // initially set to 1
        int res = 1;
        for (int i = 0; i < arr.length; i++) {

            // If we have found 'res' in the array,
            // 'res' is no longer missing, so increment it
            if (arr[i] == res) {
                res++;
            }

            // If the current element is larger than 'res',
            // 'res' cannot be found in the array,
            // so it is our final answer
            else if (arr[i] > res) {
                break;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        System.out.println(missingNumber(arr));
    }
}
Python
def missingNumber(arr):
    arr.sort()

    # res will hold the current smallest missing number,
    # initially set to 1
    res = 1
    for num in arr:

        # If we have found 'res' in the array,
        # 'res' is no longer missing, so increment it
        if num == res:
            res += 1

        # If the current element is larger than 'res',
        # 'res' cannot be found in the array,
        # so it is our final answer
        elif num > res:
            break
    return res


if __name__ == "__main__":

    arr = [2, -3, 4, 1, 1, 7]
    print(missingNumber(arr))
C#
using System;
class GfG {
    
    static int missingNumber(int[] arr) {
        Array.Sort(arr);

        // res will hold the current smallest missing number,
        // initially set to 1
        int res = 1;
        for (int i = 0; i < arr.Length; i++) {

            // If we have found 'res' in the array,
            // 'res' is no longer missing, so increment it
            if (arr[i] == res) {
                res++;
            }

            // If the current element is larger than 'res',
            // 'res' cannot be found in the array,
            // so it is our final answer
            else if (arr[i] > res) {
                break;
            }
        }
        return res;
    }

    static void Main(string[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        Console.WriteLine(missingNumber(arr));
    }
}
JavaScript
function missingNumber(arr) {
    arr.sort((a, b) => a - b);

    // ans will hold the current smallest missing number,
    // initially set to 1
    let res = 1;
    for (let i = 0; i < arr.length; i++) {

        // If we have found 'res' in the array,
        // 'res' is no longer missing, so increment it
        if (arr[i] == res) {
            res++;
        }

        // If the current element is larger than 'res',
        // 'res' cannot be found in the array,
        // so it is our final answer
        else if (arr[i] > res) {
            break;
        }
    }
    return res;
}

const arr = [2, -3, 4, 1, 1, 7];
console.log(missingNumber(arr));

Output
3

[Better approach] Using Visited Array - O(n) Time and O(n) Space

The idea is to create a visited array, to keep track of which numbers from the original array were present. For each positive number in the input array, we mark its corresponding position in the visited array. After that, we go through the visited array to find the first position that isn’t visited. The first unvisited index tells us the first missing positive number.
Note that we are marking numbers within the range from 1 to n only.

C++
#include <iostream>
#include <vector>
using namespace std;

int missingNumber(vector<int> &arr) {
    int n = arr.size();

    // To mark the occurrence of elements
    vector<bool> vis(n, false);
    for (int i = 0; i < n; i++) {

        // if element is in range from 1 to n
        // then mark it as visited
        if (arr[i] > 0 && arr[i] <= n)
            vis[arr[i] - 1] = true;
    }

    // Find the first element which is unvisited
    // in the original array
    for (int i = 1; i <= n; i++) {
        if (!vis[i - 1]) {
            return i;
        }
    }

    // if all elements from 1 to n are visited
    // then n + 1 will be first positive missing number
    return n + 1;
}

int main() {

    vector<int> arr = {2, -3, 4, 1, 1, 7};
	cout << missingNumber(arr);
}
C
#include <stdio.h>

int missingNumber(int *arr, int n) {
    
    // To mark the occurrence of elements
    int *vis = (int *)calloc(n, sizeof(int));
    
    for (int i = 0; i < n; i++) {
        
        // if element is in range from 1 to n
        // then mark it as visited
        if (arr[i] > 0 && arr[i] <= n)
            vis[arr[i] - 1] = 1;
    }

    // Find the first element which is unvisited
    // in the original array
    for (int i = 1; i <= n; i++) {
        if (!vis[i - 1]) {
            free(vis);
            return i;
        }
    }

    // if all elements from 1 to n are visited
    // then n+1 will be first positive missing number
    free(vis);
    return n + 1;
}

int main() {
    int arr[] = {2, -3, 4, 1, 1, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", missingNumber(arr, n));
    return 0;
}
Java
import java.util.Arrays;

class GfG {
    
    static int missingNumber(int[] arr) {
        int n = arr.length;

        // To mark the occurrence of elements
        boolean[] vis = new boolean[n];
        for (int i = 0; i < n; i++) {

            // if element is in range from 1 to n
            // then mark it as visited
            if (arr[i] > 0 && arr[i] <= n)
                vis[arr[i] - 1] = true;
        }

        // Find the first element which is unvisited
        // in the original array
        for (int i = 1; i <= n; i++) {
            if (!vis[i - 1]) {
                return i;
            }
        }

        // if all elements from 1 to n are visited
        // then n+1 will be first positive missing number
        return n + 1;
    }

    public static void main(String[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        System.out.println(missingNumber(arr));
    }
}
Python
def missingNumber(arr):
    n = len(arr)

    # To mark the occurrence of elements
    vis = [False] * n
    for i in range(n):

        # if element is in range from 1 to n
        # then mark it as visited
        if 0 < arr[i] <= n:
            vis[arr[i] - 1] = True

    # Find the first element which is unvisited
    # in the original array
    for i in range(1, n + 1):
        if not vis[i - 1]:
            return i

    # if all elements from 1 to n are visited
    # then n+1 will be first positive missing number
    return n + 1

if __name__ == "__main__":
    arr = [2, -3, 4, 1, 1, 7]
    print(missingNumber(arr))
C#
using System;

class GfG {
    static int missingNumber(int[] arr) {
        int n = arr.Length;

        // To mark the occurrence of elements
        bool[] vis = new bool[n];
        for (int i = 0; i < n; i++) {

            // if element is in range from 1 to n
            // then mark it as visited
            if (arr[i] > 0 && arr[i] <= n)
                vis[arr[i] - 1] = true;
        }

        // Find the first element which is unvisited
        // in the original array
        for (int i = 1; i <= n; i++) {
            if (!vis[i - 1]) {
                return i;
            }
        }

        // if all elements from 1 to n are visited
        // then n+1 will be first positive missing number
        return n + 1;
    }

    static void Main() {
        int[] arr = { 2, -3, 4, 1, 1, 7 };
        Console.WriteLine(missingNumber(arr));
    }
}
Javascript
function missingNumber(arr) {
    let n = arr.length;

    // To mark the occurrence of elements
    let vis = new Array(n).fill(false);
    for (let i = 0; i < n; i++) {

        // if element is in range from 1 to n
        // then mark it as visited
        if (arr[i] > 0 && arr[i] <= n)
            vis[arr[i] - 1] = true;
    }

    // Find the first element which is unvisited
    // in the original array
    for (let i = 1; i <= n; i++) {
        if (!vis[i - 1]) {
            return i;
        }
    }

    // if all elements from 1 to n are visited
    // then n+1 will be first positive missing number
    return n + 1;
}

const arr = [2, -3, 4, 1, 1, 7];
console.log(missingNumber(arr));

Output
3

[Expected Approach] Using Cycle Sort - O(n) Time and O(1) Space

The idea is similar to Cycle Sort and move each element in the array to its correct position based on its value. So for each number, say x, such that 1 ≤ x ≤ n, is placed at the (x - 1)th index.

Finally, iterate through the array and check if the numbers are in the expected indexes or not. The first place where the number doesn’t match its index gives us the first missing positive number. If all the numbers from 1 to n, are at their correct indexes, then the next number i.e., n + 1, is the smallest missing positive number.

C++
#include <iostream>
#include <vector>
using namespace std;

int missingNumber(vector<int> &arr) {
    int n = arr.size();

    for (int i = 0; i < n; i++) {

        // if arr[i] is within range [1, n] and arr[i] is
        // not placed at (arr[i]-1)th index in arr
        while (arr[i] >= 1 && arr[i] <= n
               && arr[i] != arr[arr[i] - 1]) {
                   
            // then swap arr[i] and arr[arr[i]-1]
            // to place arr[i] to its corresponding index
            swap(arr[i], arr[arr[i] - 1]);
        }
    }

    // If any number is not at its corresponding index 
    // then it is the missing number
    for (int i = 1; i <= n; i++) {
        if (i != arr[i-1]) {
            return i;
        }
    }

    // If all number from 1 to n are present 
    // then n + 1 is smallest missing number
    return n + 1;
}

int main() {
    
    vector<int> arr = {2, -3, 4, 1, 1, 7};
    cout << missingNumber(arr);
    return 0;
}
C
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int missingNumber(int arr[], int n) {

    for (int i = 0; i < n; i++) {

        // if arr[i] is within range [1, n] and arr[i] is
        // not placed at (arr[i]-1)th index in arr
        while (arr[i] >= 1 && arr[i] <= n 
               && arr[i] != arr[arr[i] - 1]) {
                   
            // then swap arr[i] and arr[arr[i]-1]
            // to place arr[i] to its corresponding index
            swap(&arr[i], &arr[arr[i] - 1]);
        }
    }

    // If any number is not at its corresponding index 
    // then it is the missing number
    for (int i = 1; i <= n; i++) {
        if (i != arr[i-1]) {
            return i;
        }
    }

    // If all number from 1 to n are present 
    // then n + 1 is smallest missing number
    return n + 1;
}

int main() {
    int arr[] = {2, -3, 4, 1, 1, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", missingNumber(arr, n));
    return 0;
}
Java
import java.util.*;

class GfG {

    static int missingNumber(int[] arr) {

        int n = arr.length;
        for (int i = 0; i < n; i++) {

            // if arr[i] is within the range [1, n] and arr[i]
            // is not placed at (arr[i]-1)th index in arr
            while (arr[i] >= 1 && arr[i] <= n
                   && arr[i] != arr[arr[i] - 1]) {

                // then swap arr[i] and arr[arr[i]-1] to
                // place arr[i] to its corresponding index
                int temp = arr[i];
                arr[i] = arr[arr[i] - 1];
                arr[temp - 1] = temp;
            }
        }

        // If any number is not at its corresponding index 
        // then it is the missing number
        for (int i = 1; i <= n; i++) {
            if (i != arr[i - 1]) {
                return i;
            }
        }

        // If all number from 1 to n are present then n+1 
        // is smallest missing number
        return n + 1;
    }

    public static void main(String[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        System.out.println(missingNumber(arr));
    }
}
Python
def missingNumber(arr):

    n = len(arr)
    for i in range(n):

        # if arr[i] is within the range 1 to n
        # and arr[i] is not placed at (arr[i]-1)th index in arr
        while 1 <= arr[i] <= n and arr[i] != arr[arr[i] - 1]:

            # then swap arr[i] and arr[arr[i]-1] to place arr[i]
            # to its corresponding index
            temp = arr[i]
            arr[i] = arr[arr[i] - 1]
            arr[temp - 1] = temp

    # If any number is not at its corresponding index, then it
    # is the missing number
    for i in range(1, n + 1):
        if i != arr[i - 1]:
            return i

    # If all number from 1 to n are present 
    # then n + 1 is smallest missing number
    return n + 1

if __name__ == '__main__':
    arr = [2, -3, 4, 1, 1, 7]
    print(missingNumber(arr))
C#
using System;

class GfG {

    static int missingNumber(int[] arr) {
        
        int n = arr.Length;
        for (int i = 0; i < n; i++) {

            // if arr[i] is within the range 1 to n and arr[i]
            // is not placed at (arr[i]-1)th index in arr
            while (arr[i] >= 1 && arr[i] <= n
                   && arr[i] != arr[arr[i] - 1]) {

                // then swap arr[i] and arr[arr[i]-1]
                // to place arr[i] to its corresponding index
                int temp = arr[i];
                arr[i] = arr[arr[i] - 1];
                arr[temp - 1] = temp;
            }
        }

        // If any number is not at its corresponding index 
        // then it is the missing number
        for (int i = 1; i <= n; i++) {
            if (i != arr[i - 1]) {
                return i;
            }
        }

        // If all number from 1 to n are present 
        // then n+1 is smallest missing number
        return n + 1;
    }

    static void Main() {
        int[] arr = { 2, -3, 4, 1, 1, 7 };
        Console.WriteLine(missingNumber(arr));
    }
}
Javascript
function missingNumber(arr) {

    let n = arr.length;
    for (let i = 0; i < n; i++) {

        // if arr[i] is within the range 1 to n and arr[i] is
        // not placed at (arr[i]-1)th index in arr
        while (arr[i] >= 1 && arr[i] <= n 
               && arr[i] !== arr[arr[i] - 1]) {

            // then swap arr[i] and arr[arr[i]-1] to place 
            // arr[i] to its corresponding index
            let temp = arr[i];
            arr[i] = arr[arr[i] - 1];
            arr[temp - 1] = temp;
        }
    }

    // If any number is not at its corresponding index 
    // it is then missing,
    for (let i = 1; i <= n; i++) {
        if (i !== arr[i - 1]) {
            return i;
        }
    }

    // If all number from 1 to n are present 
    // then n+1 is smallest missing number
    return n + 1;
}

let arr = [2, -3, 4, 1, 1, 7];
console.log(missingNumber(arr));

Output
3

[Alternate Approach - 1] By Negating Array Elements – O(n) Time and O(1) Space

The main idea of this approach is to first move all positive integers to the left side of the array. Then, for the left segment, it marks the presence of a number val (where 1 ≤ val ≤ k) by making the element at index (val - 1) negative. This marking helps identify which numbers are present. Finally, it scans the marked segment to find the first index that remains positive, indicating the smallest missing positive number. If all positions are marked, the missing number is k + 1.

C++
#include <iostream>
#include <vector>
using namespace std;

int partition(vector<int> &arr) {
    int pivotIdx = 0;
    int n = arr.size();

    for (int i = 0; i < n; i++) {
        
        // Move positive elements to the left
        if (arr[i] > 0) {
            swap(arr[i], arr[pivotIdx]);
            pivotIdx++;
        }
    }

    // return index of the first non-positive number
    return pivotIdx;
}

int missingNumber(vector<int> &arr) {
    int k = partition(arr);
    
    // Traverse the positive part of the array
    for (int i = 0; i < k; i++) {
        
        // Find the absolute value to get the original number
        int val = abs(arr[i]);

        // If val is within range, then mark the element at
        // index val-1 to negative
        if (val - 1 < k && arr[val - 1] > 0) {
            arr[val - 1] = -arr[val - 1];
            
        }
    }
    
    // Find first unmarked index
    for (int i = 0; i < k; i++) {
        
        if (arr[i] > 0) {
            return i + 1;
        }
    }
    
    // If all numbers from 1 to k are marked
    // then missing number is k + 1
    return k + 1;
}

int main() {
    
    vector<int> arr = {2, -3, 4, 1, 1, 7};
    cout << missingNumber(arr);
    return 0;
}
C
#include <stdio.h>

int partition(int *arr, int n) {
    int pivotIdx = 0;

    for (int i = 0; i < n; i++) {
        
        // Move positive elements to the left
        if (arr[i] > 0) {
            int temp = arr[i];
            arr[i] = arr[pivotIdx];
            arr[pivotIdx] = temp;
            pivotIdx++;
        }
    }

    // return index of the first non-positive number
    return pivotIdx;
}

int missingNumber(int *arr, int n) {
    int k = partition(arr, n);

    // Traverse the positive part of the array
    for (int i = 0; i < k; i++) {
        
        // Find the absolute value to get the original number
        int val = abs(arr[i]);

        // If val is within range, then mark the element at
        // index val-1 to negative
        if (val - 1 < k && arr[val - 1] > 0) {
            arr[val - 1] = -arr[val - 1];
        }
    }

    // Find first unmarked index
    for (int i = 0; i < k; i++) {
        if (arr[i] > 0) {
            return i + 1;
        }
    }

    // If all numbers from 1 to k are marked then
    // missing number is k + 1
    return k + 1;
}

int main() {
 
    int arr[] = {2, -3, 4, 1, 1, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", missingNumber(arr, n));
    return 0;
}
Java
import java.util.Arrays;

class GfG {

    static int partition(int[] arr) {
        int pivotIdx = 0;
        int n = arr.length;

        for (int i = 0; i < n; i++) {
            
            // Move positive elements to the left
            if (arr[i] > 0) {
                int temp = arr[i];
                arr[i] = arr[pivotIdx];
                arr[pivotIdx] = temp;
                pivotIdx++;
            }
        }

        // return index of the first non-positive number
        return pivotIdx;
    }

    static int missingNumber(int[] arr) {
        int k = partition(arr);

        // Traverse the positive part of the array
        for (int i = 0; i < k; i++) {
            
            // Find the absolute value to get the original number
            int val = Math.abs(arr[i]);

            // If val is within range, then mark the element at
            // index val-1 to negative
            if (val - 1 < k && arr[val - 1] > 0) {
                arr[val - 1] = -arr[val - 1];
            }
        }

        // Find first unmarked index
        for (int i = 0; i < k; i++) {
            if (arr[i] > 0) {
                return i + 1;
            }
        }

        // If all numbers from 1 to k are marked
        // then missing number is k + 1
        return k + 1;
    }

    public static void main(String[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        System.out.println(missingNumber(arr));
    }
}
Python
def partition(arr):
    pivotIdx = 0
    n = len(arr)

    for i in range(n):
        
        # Move positive elements to the left
        if arr[i] > 0:
            arr[i], arr[pivotIdx] = arr[pivotIdx], arr[i]
            pivotIdx += 1

    # return index of the first non-positive number
    return pivotIdx

def missingNumber(arr):
    k = partition(arr)

    # Traverse the positive part of the array
    for i in range(k):
        
        # Find the absolute value to get the original number
        val = abs(arr[i])

        # If val is within range, then mark the element at
        # index val-1 to negative
        if val - 1 < k and arr[val - 1] > 0:
            arr[val - 1] = -arr[val - 1]

    # Find first unmarked index
    for i in range(k):
        if arr[i] > 0:
            return i + 1

    # If all numbers from 1 to k are marked
    # then missing number is k + 1
    return k + 1

if __name__ == "__main__":
    arr = [2, -3, 4, 1, 1, 7]
    print(missingNumber(arr))
C#
using System;

class GfG {
  
    static int Partition(int[] arr) {
        int pivotIdx = 0;
        int n = arr.Length;

        for (int i = 0; i < n; i++) {
          
            // Move positive elements to the left
            if (arr[i] > 0) {
                int temp = arr[i];
                arr[i] = arr[pivotIdx];
                arr[pivotIdx] = temp;
                pivotIdx++;
            }
        }

        // return index of the first non-positive number
        return pivotIdx;
    }

    static int missingNumber(int[] arr) {

        int k = Partition(arr);

        // Traverse the positive part of the array
        for (int i = 0; i < k; i++) {
            
            // Find the absolute value to get the original number
            int val = Math.Abs(arr[i]);

            // If val is within range, then mark the element at
            // index val-1 to negative
            if (val - 1 < k && arr[val - 1] > 0)
                arr[val - 1] = -arr[val - 1];
        }

        // Find first unmarked index
        for (int i = 0; i < k; i++) {
            if (arr[i] > 0) {
                return i + 1;
            }
        }

        // If all numbers from 1 to k are marked
        // then missing number is k + 1
        return k + 1;
    }

    static void Main() {
        int[] arr = { 2, -3, 4, 1, 1, 7 };
        Console.WriteLine(missingNumber(arr));
    }
}
JavaScript
function partition(arr) {
    let pivotIdx = 0;
    const n = arr.length;

    for (let i = 0; i < n; i++) {
        
        // Move positive elements to the left
        if (arr[i] > 0) {
            [arr[i], arr[pivotIdx]] = [arr[pivotIdx], arr[i]];
            pivotIdx++;
        }
    }

    // return index of the first non-positive number
    return pivotIdx;
}

function missingNumber(arr) {
    const k = partition(arr);

    // Traverse the positive part of the array
    for (let i = 0; i < k; i++) {
        
        // Find the absolute value to get the original number
        const val = Math.abs(arr[i]);

        // If val is within range, then mark the element at
        // index val-1 to negative
        if (val - 1 < k && arr[val - 1] > 0) {
            arr[val - 1] = -arr[val - 1];
        }
    }

    // Find first unmarked index
    for (let i = 0; i < k; i++) {
        if (arr[i] > 0) {
            return i + 1;
        }
    }

    // If all numbers from 1 to k are marked
    // then missing number is k + 1
    return k + 1;
}

// Driver Code
const arr = [2, -3, 4, 1, 1, 7];
console.log(missingNumber(arr));

Output
3

[Alternate Approach - 2] By Marking Indices – O(n) Time and O(1) Space

The idea is to first check if 1 is present in the array or not. If not, the answer is 1. Otherwise, replace all the numbers outside the range [1, n] to 1. Then, iterate over the array again and mark the occurrences of each number, say x by adding n to index x - 1.Lastly, iterate over the array again to find the missing element by searching for the first unmarked index.

C++
#include <iostream>
#include <vector>
using namespace std;

int missingNumber(vector<int> &arr) {
  
  	int n = arr.size();
    bool flag = false;

    // Check if 1 is present in array or not
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1) {
            flag = true;
            break;
        }
    }

    // If 1 is not present
    if (flag == false)
        return 1;

    // Change out of range values to 1
    for (int i = 0; i < n; i++) {
        if (arr[i] <= 0 || arr[i] > n)
            arr[i] = 1;
        
    }

    // Mark the occurrence of numbers 
  	// directly within the same array
    for (int i = 0; i < n; i++) {
        arr[(arr[i] - 1) % n] += n;
    }

    // Finding which index has value less than n
    for (int i = 0; i < n; i++) {
        if (arr[i] <= n)
            return i + 1;
    }

    // If array has values from 1 to n
    return n + 1;
}

int main() {
    vector<int> arr = {2, -3, 4, 1, 1, 7};
  
    cout << missingNumber(arr);
	return 0;
}
C
#include <stdio.h>

int missingNumber(int arr[], int n) {
    int flag = 0;

    // Check if 1 is present in array or not
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1) {
            flag = 1;
            break;
        }
    }

    // If 1 is not present
    if (flag == 0)
        return 1;

    // Change out of range values to 1
    for (int i = 0; i < n; i++) {
        if (arr[i] <= 0 || arr[i] > n)
            arr[i] = 1;
    }

    // Mark the occurrence of numbers 
    // directly within the same array
    for (int i = 0; i < n; i++) {
        arr[(arr[i] - 1) % n] += n;
    }

    // Finding which index has value less than n
    for (int i = 0; i < n; i++) {
        if (arr[i] <= n)
            return i + 1;
    }

    // If array has values from 1 to n
    return n + 1;
}

int main() {
    int arr[] = {2, -3, 4, 1, 1, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
  
    printf("%d", missingNumber(arr, n));
    return 0;
}
Java
import java.util.*;
class GfG {

    static int missingNumber(int[] arr) {
        int n = arr.length;
        boolean flag = false;

        // Check if 1 is present in array or not
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) {
                flag = true;
                break;
            }
        }

        // If 1 is not present
        if (!flag)
            return 1;

        // Change out of range values to 1
        for (int i = 0; i < n; i++) {
            if (arr[i] <= 0 || arr[i] > n)
                arr[i] = 1;
        }

        // Mark the occurrence of numbers 
        // directly within the same array
        for (int i = 0; i < n; i++) {
            arr[(arr[i] - 1) % n] += n;
        }

        // Finding which index has value less than n
        for (int i = 0; i < n; i++) {
            if (arr[i] <= n)
                return i + 1;
        }

        // If array has values from 1 to n
        return n + 1;
    }

    public static void main(String[] args) {
        int[] arr = {2, -3, 4, 1, 1, 7};
        System.out.println(missingNumber(arr));
    }
}
Python
def missingNumber(arr):
    n = len(arr)
    flag = False

    # Check if 1 is present in array or not
    for i in range(n):
        if arr[i] == 1:
            flag = True
            break

    # If 1 is not present
    if not flag:
        return 1

    # Change out of range values to 1
    for i in range(n):
        if arr[i] <= 0 or arr[i] > n:
            arr[i] = 1

    # Mark the occurrence of numbers 
    # directly within the same array
    for i in range(n):
        arr[(arr[i] - 1) % n] += n

    # Finding which index has value less than n
    for i in range(n):
        if arr[i] <= n:
            return i + 1

    # If array has values from 1 to n
    return n + 1

if __name__ == "__main__":
    arr = [2, -3, 4, 1, 1, 7]
    print(missingNumber(arr))
C#
using System;

class GfG {

    static int missingNumber(int[] arr) {
        int n = arr.Length;
        bool flag = false;

        // Check if 1 is present in array or not
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) {
                flag = true;
                break;
            }
        }

        // If 1 is not present
        if (!flag)
            return 1;

        // Change out of range values to 1
        for (int i = 0; i < n; i++) {
            if (arr[i] <= 0 || arr[i] > n)
                arr[i] = 1;
        }

        // Mark the occurrence of numbers 
        // directly within the same array
        for (int i = 0; i < n; i++) {
            arr[(arr[i] - 1) % n] += n;
        }

        // Finding which index has value less than n
        for (int i = 0; i < n; i++) {
            if (arr[i] <= n)
                return i + 1;
        }

        // If array has values from 1 to n
        return n + 1;
    }

    static void Main() {
        int[] arr = {2, -3, 4, 1, 1, 7};
        Console.WriteLine(missingNumber(arr));
    }
}
JavaScript
function missingNumber(arr) {
    let n = arr.length;
    let flag = false;

    // Check if 1 is present in array or not
    for (let i = 0; i < n; i++) {
        if (arr[i] === 1) {
            flag = true;
            break;
        }
    }

    // If 1 is not present
    if (!flag) return 1;

    // Change out of range values to 1
    for (let i = 0; i < n; i++) {
        if (arr[i] <= 0 || arr[i] > n) arr[i] = 1;
    }

    // Mark the occurrence of numbers 
    // directly within the same array
    for (let i = 0; i < n; i++) {
        arr[(arr[i] - 1) % n] += n;
    }

    // Finding which index has value less than n
    for (let i = 0; i < n; i++) {
        if (arr[i] <= n) return i + 1;
    }

    // If array has values from 1 to n
    return n + 1;
}

let arr = [2, -3, 4, 1, 1, 7];
console.log(missingNumber(arr));

Output
3



First Missing Positive
Visit Course explore course icon

Similar Reads