Smallest positive number missing in an unsorted array

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

In the given unsorted array find the smallest missing positive integer. Positive integer doesn`t include 0. We can modify the original array if needed.

Array may contain positive and negative numbers.


a. Input array : [3, 4, -1, 0, -2, 2, 1, 7, -8]

Output : 5.
In the given array 1,2,3,4 are present but 5 is absent, which is the smallest positive number missin in the given array.

b. Input array : [2, 3, 7, 8, -1, -2 ,0]

Output : 1.
In the given array 1 is absent, which is the smallest positive number missing in the given array.

c. Input array : [2, 3, 7, 8, -1, -2 ,0]

Output : 1.
In the given array 1 is absent, which is the smallest positive number missing in the given array.


a. We need to find the smallest missing positive number, so we don’t need negative numbers. So, we create a function to move all the positive numbers to the end of the array and we find missing number in the positive array.

b. In positive_array function,

1. We traverse the array with two pointers, if we find a negative number we swap it with the start pointer and move it forward.

2. We continue this until it reaches the end of the array and we return the start index of the positive array. Which is the start pointer position, when we reach the end of the array.

c. We create a FindMissingPostive function which takes the array and size of the array and gives the smallest missing number.

d. In this function,

1. In the input array, if we find a positive integer, we mark it visited my making the value at its index position to negative. Example: If given array is array[]. In this array, if we find 2 we make array[1]  = -2 and so on till the end of array only if array[i] - 1< size and array[array[i] – 1]> 0.

2. After modifying the array. If index at which we find the first positive number is k. Then k+1 is the smallest missing number. If we didn’t find a positive number then, size of the array + 1 is the smallest missing number.

e. For the given input array, we first apply positive_arrayfunction(let it return j)and we apply the  FindMissingPostive on (array+j).

Algorithm Working

C++ Program

#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>

using namespace std;
//function to swap addresses
void swap(int *a, int *b)
    int temp = *a;
    *a   = *b;
    *b   = temp;

//In this function we move non-postive elements to the start of the array
//return the start index of start index of postive array
int positive_array(int array[], int N)
    int start_index = 0, i;
    for(i = 0; i < N; i++)
       if (array[i] <= 0)  
           swap(&array[i], &array[start_index]);
           start_index++;  // increment count of non-positive integers
    return start_index;

//In the given array this function finds the smallest missing number
//N is size of the array
//we mark the elements by making the elements at the postion(i-1)to negitive
//after modifying the array we find index at which we find first postive(j) and j+1 is the missing number.
int FindMissingPostive(int array[], int N)
  for(int i = 0; i < N; i++)
    if(abs(array[i]) < N+1 && array[abs(array[i]) - 1] > 0)
      array[abs(array[i]) - 1] = -array[abs(array[i]) - 1];
  for(int k = 0; k < N; k++){
    if (array[k] > 0)
      return k+1;

  return N+1;
//insted of finding the missing postive integer in whole array
//we can find in postive part of the array fast.
int FindMissing(int array[], int N)
   int start = positive_array(array,N);
   return FindMissingPostive(array+start,N-start);

//main function to test above functions 
int main()
  int input_array[] = {2,3,-4,-6,1,7};
  int N = sizeof(input_array)/sizeof(int);//size of the array
  cout<<"smallest positive number missing: = "<<FindMissing(input_array,N);
  return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top