πDay 13. Smallest Positive Missing Number π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You are given an integer array arr[]. Your task is to find the smallest positive number missing from the array.
Note: Positive number starts from 1. The array can have negative integers too.
π Example Walkthrough:
Input:
arr[] = [2, -3, 4, 1, 1, 7]
Output:
3
Explanation: Smallest positive missing number is 3.
Input:
arr[] = [5, 3, 2, 5, 1]
Output:
4
Explanation: Smallest positive missing number is 4.
Input:
arr[] = [-8, 0, -1, -4, -3]
Output:
1
Explanation: Smallest positive missing number is 1.
Constraints:
$
1 <= arr.size() <= 10^5$$
-10^6 <= arr[i] <= 10^6$
π― My Approach:
In-place Rearrangement:
The problem can be solved using an in-place rearrangement technique that places elements at their correct indices.
The idea is to rearrange the elements such that for any element
arr[i], it should be placed at indexarr[i] - 1.After rearranging the elements, traverse the array again to find the smallest missing positive integer.
Steps:
Iterate through the array, and for each element that is within the valid range
[1, n], place it in its correct position.Once the array is rearranged, traverse the array to identify the smallest index
iwherearr[i] != i + 1. This indicates the missing number.If all elements are in place, the missing number is
n + 1.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where
nis the size of the array. The algorithm requires two linear scans of the array, making it efficient.Expected Auxiliary Space Complexity: O(1), as we use only a constant amount of additional space.
π Solution Code
Code (C)
Code (Cpp)
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated