Find Missing Positive in Array in O(n) Time

🚀 Day 30 / 100 | First Missing Positive -Intuition: The smallest missing positive will always be in range 1 to n+1. Place each number at its correct index (1 at index 0, 2 at index 1 so on). First index where value ≠ index+1 gives the missing number. -Approach : O(n) Find length of array n Traverse array and place each element at correct position -> index = value − 1 Ignore negative numbers, zero, and numbers > n After placement, traverse array again If arr[i] ≠ i+1 -> return i+1 If all elements are correct -> return n+1 -Complexity: Time Complexity : O(n) Space Complexity : O(1) #100DaysOfCode #Java #DSA #Array #LeetCode

  • text

To view or add a comment, sign in

Explore content categories