logo CodeStepByStep logo

firstMissingPositive

Language/Type: Java arrays

Write a method named firstMissingPositive that accepts an unsorted array of integers as a parameter and returns the first positive integer value that is not present in the array. For example, if an array called a stores {4, -6, 5, 2, 1}, then the call of firstMissingPositive(a) should return 3. If the array is empty or contains no positive integers, return 1.

An optimal solution runs in O(N) time and uses a constant amount of extra space. You may change the array contents in your solution if you find it helpful to do so.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.