Prepin
Log in

Smallest Absent Integer

Approach / Explanation

In the Smallest Missing Integer problem, you are generally presented with an unsorted array of integers, and your objective is to identify the smallest positive integer (greater than 0) that is absent from that array. The array can vary in length, and the integers may include negative values and be in any order. The goal is to devise an efficient algorithm to determine the smallest missing integer. Here are some important points to consider when tackling the problem: Positive and negative integers can be addressed separately, as negative integers are irrelevant when searching for the smallest positive integer. The missing integer will fall within the range of 1 to array.length + 1, so other integers in the array can be disregarded. One approach is to sort the array and then perform a linear scan to locate the first missing positive integer (though this method is not advisable due to its potential inefficiency and high resource usage). A more optimal solution can be found by marking the index that corresponds to each number in the array as you traverse it, using the array's indices to indicate the presence of integer values.

Suggested Answer

A potential approach to addressing the issue using Python could be: def find_smallest_missing_integer ( nums ) : if not nums : return 1 n = len ( nums ) for i in range ( n ) : while 1 <= nums [ i ] <= n and nums [ nums [ i ] - 1 ] != nums [ i ] : nums [ nums [ i ] - 1 ] , nums [ i ] = nums [ i ] , nums [ nums [ i ] - 1 ] for i in range ( n ) : if nums [ i ] != i + 1 : return i + 1 return n + 1 This method starts by addressing the base case when the input array is empty. Next, it rearranges the elements in the array (if they fall within the range of 1 to n) to align with their respective indices. Finally, by checking each index against the expected value, it identifies the smallest missing positive integer.

Alternative Answer

Here’s an alternative solution implemented in Java: public int smallestMissingInteger ( int [ ] A ) { int n = A . length ; for ( int i = 0 ; i < n ; i ++ ) { while ( A [ i ] > 0 && A [ i ] <= n && A [ A [ i ] - 1 ] != A [ i ] ) { int tmp = A [ i ]; A [ i ] = A [ A [ i ] - 1 ]; A [ tmp - 1 ] = tmp ; } } for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] != i + 1 ) return i + 1 ; return n + 1 ; } This Java implementation functions in the same manner as the Python version. It begins by verifying if the current number being evaluated is positive and within the bounds of the array's length. If this condition is met, it swaps the current number with the one located at the index corresponding to its value. Finally, it iterates through the array again to identify the first index that does not have the expected number, which is then returned as the smallest missing positive integer. If no such index is located, it returns the length of the array plus one.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.