Solved LeetCode 41: First Missing Positive in O(n) time

Day 47/100 – #100DaysOfCode 🚀 | #Java #Arrays #InPlaceAlgorithms ✅ Problem Solved: First Missing Positive (LeetCode 41) 🧩 Problem Summary: Given an unsorted array, find the smallest missing positive integer. The solution must run in O(n) time and constant extra space. 💡 Approach Used: This is not a direct sorting or hashing problem — the key is index positioning. Logic: The answer must lie within 1 to n+1. Place each positive number x at index x-1 if possible. Then scan to find the first index where nums[i] != i+1. Steps: Iterate and swap values into their correct position. Ignore values ≤0 or >n. Final scan to find the first missing positive. ⚙️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Takeaway: This problem teaches how index-based hashing can eliminate the need for extra space — very common in interview-style optimization problems. #Java #LeetCode #Algorithm #Array #ProblemSolving #100DaysOfCode #CodingChallenge

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories