Write a program in C++ to find the missing positive number in a given array of unsorted integers



We are given an array of unsorted integers, and the task is to find the missing positive integer. The given array may contain negative numbers, zeros, and duplicate values, all in any random order.

Let's look at some example scenarios to understand the problem clearly-

Scenario 1-

Input: arr = [3, -2, 5, 1, -7, 4, -1, 8]
Output: 2

Explanation:
The number 2 is not present in the array, so the missing positive is 2

Scenario 2-

Input: arr = [0]
Output: 1

Explanation:
In the given array, '1' is the only positive integer that is missing, thus the output is '1'.

Finding Missing Positive Number in a Given Array of Unsorted Integers in C++

To find the smallest missing positive number in an array, we can use the following methods-

Naive Approach

In this naive approach, we first sort the given array in ascending order using the C++ built-in sort() function. Then, we start a loop to check for the missing positive number.

Example

Following is a complete C++ program where we sort the array and then start with initializing smallest = 1. While scanning the array, if the current element matches smallest, we increase smallest. At the end, the value of smallest will be the missing number.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to find the missing positive number using sorting
int missingNumberWithSort(vector<int>& values) {
    sort(values.begin(), values.end());
    int smallest = 1;

    for (int num : values) {
        if (num == smallest) {
            smallest++;
        }
    }
    return smallest;
}

int main() {
    vector<int> inputArray = {4, 5, -1, 0};

    cout << "Input array: ";
    for (int val : inputArray) {
        cout << val << " ";
    }
    cout << endl;
    int result = missingNumberWithSort(inputArray);
    cout << "Missing positive number: " << result << endl;
    return 0;
}

Following is the output of the above program, which displays the missing positive number.

Input array: 4 5 -1 0 
Missing positive number: 1

Time Complexity: O(n log n) because of sorting.

Space Complexity: O(1) because sorting happens in place.

Using Hashing

Hashing is a technique used to store and find data where each data item has a unique key. In this method, we insert all positive numbers from the array into an unordered set. Then, starting from 1, we check sequentially using the set and return the first number that is not in it as the missing positive integer.

Example

Following is a complete C++ program where we print the missing positive number using an unordered set.

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

// Function to find the missing positive number using hashing
int missingNumberWithHash(vector<int>& values) {
    unordered_set<int> seen;
    for (int num : values) {
        if (num > 0) 
            seen.insert(num);
    }

    int smallest = 1;
    while (true) {
        if (seen.find(smallest) == seen.end())
            return smallest;
        smallest++;
    }
}

int main() {
    vector<int> inputArray = {4, 5, -1, 0};
    cout << "Input array: ";
    for (int val : inputArray) {
        cout << val << " ";
    }
    cout << endl;
    int result = missingNumberWithHash(inputArray);
    cout << "Missing positive number: " << result << endl;
    return 0;
}

The output of the above program is shown below, which displays the missing positive number.

Input array: 4 5 -1 0 
Missing positive number: 1

Time Complexity: O(n) because each element is processed once.

Space Complexity: O(n) because we store elements in a hash set.

Index Mapping Approach

This method rearranges numbers by placing each positive number x at index x-1. After this, we scan the array, and the first index where the number is not equal to index+1 gives the missing number. If all positions are correct, the missing number is n+1.

Example

Below is a complete C++ program where we use a while loop to place each number at its correct position and then find the missing positive number.

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

// Function to find the missing positive number using index mapping
int missingNumberWithIndexMapping(vector<int>& values) {
    int n = values.size();
    // Place each number in its correct position if possible
    for (int i = 0; i < n; i++) {
        while (values[i] > 0 && values[i] <= n && values[values[i] - 1] != values[i]) {
            swap(values[i], values[values[i] - 1]);
        }
    }
    // Identify the first missing positive
    for (int i = 0; i < n; i++) {
        if (values[i] != i + 1) {
            return i + 1;
        }
    }
    return n + 1;
}

int main() {
    vector<int> inputArray = {4, 5, -1, 0};
    cout << "Input array: ";
    for (int val : inputArray) {
        cout << val << " ";
    }
    cout << endl;
    int result = missingNumberWithIndexMapping(inputArray);
    cout << "Missing positive number: " << result << endl;
    return 0;
}

Below is the output of the above program, which displays the missing positive number.

Input array: 4 5 -1 0
Missing positive number: 1 

Time Complexity: O(n) because each element is swapped at most once.

Space Complexity: O(1) because rearrangement is done within the same array.

Conclusion

In this article we saw three different methods to find the missing positive number from a given unsorted array. We saw the naive approach, hashing method, and index mapping method. Among these, indexing is the optimal method with time complexity of O(n) and constant space complexity.

Updated on: 2025-08-20T17:19:07+05:30

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements