First Missing Positive šŸŽÆ

beginner
11 min

First Missing Positive šŸŽÆ

Welcome to this comprehensive guide on the First Missing Positive problem, a popular algorithmic concept in the field of Data Structures and Algorithms. Let's embark on this exciting journey together, exploring real-world applications and practical examples.

Understanding the Problem šŸ“

The First Missing Positive problem asks us to find the first number that is missing in a given unsorted list of numbers. The numbers in the list range from 1 to n (the number of elements in the list), and there may be duplicate numbers. The goal is to find the smallest number that is not in the list.

Algorithm Explanation šŸ’”

The solution to this problem involves iterating through the list and maintaining two arrays - one to store the frequency of each number in the list, and another to mark the numbers that have been encountered. We'll then go through the numbers again and find the first number that is not marked.

Here's an outline of the algorithm:

  1. Initialize two arrays: count[] to store the frequency of each number, and present[] to mark the numbers that have been encountered.
  2. Iterate through the list and do the following for each number num:
    • Increment count[num] if num is positive and less than or equal to n.
    • Set present[num] to 1.
  3. Iterate through the numbers from 1 to n and do the following for each number num:
    • If present[num] is 0 and count[num] is 0, then return num.

Let's dive into some code examples to make this clearer.

Code Examples šŸ“

Python Example

python
def firstMissingPositive(arr, n): # Initialize the present array with zeros present = [0] * (n + 1) # Iterate through the list for num in arr: if num > 0 and num <= n: present[num] = 1 # Iterate through the numbers from 1 to n for num in range(1, n + 1): if present[num] == 0 and arr[count[num] - 1] != num: return num # If no missing number is found, return n+1 return n + 1

Java Example

java
public int firstMissingPositive(int[] arr, int n) { int[] present = new int[n + 1]; for (int num : arr) { if (num > 0 && num <= n) { present[num] = 1; } } for (int num = 1; num <= n; num++) { if (present[num] == 0 && arr[count[num] - 1] != num) { return num; } } // If no missing number is found, return n+1 return n + 1; }

Pro Tip: You can optimize the code by using a HashSet to store the numbers instead of the present array. This will significantly improve the runtime complexity.

Practical Applications šŸŽÆ

The First Missing Positive problem has various real-world applications, such as:

  1. Inventory Management: Finding missing items in an inventory list.
  2. Network Analysis: Detecting missing nodes in a network.
  3. Data Validation: Validating data integrity in databases.

Quiz šŸ’”

Quick Quiz
Question 1 of 1

What is the goal of the First Missing Positive problem?

We hope this lesson has helped you understand the First Missing Positive problem and its applications. Keep practicing and exploring to improve your algorithmic skills! šŸ¤šŸ¼