πŸš€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:

  1. 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 index arr[i] - 1.

    • After rearranging the elements, traverse the array again to find the smallest missing positive integer.

  2. 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 i where arr[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 n is 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