Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

Can I modify the array?

0 votes
411 views

like putting interger into the i-th element of the array

asked Dec 21, 2013 in First Missing Positive by makeittrue (180 points)

Can you explain you solution? I can't think of a way to modify the array and reach the solution. Thanks :)

I think it is the only way to satisfy the O(n) time and O(1) space requirement. Observed that for a size n array, it can store only values 1..n. If are these elements are present, then next positive is n+1. Otherwise, if we put the things into the "right" place in the array, then the missing element will not be in place. So you can scan the array and see if A[i] == i+1. return i+1 if not, or return n+1 if no such i exist.

2 Answers

0 votes

That's the only I've found to respect the complexity requirements.

answered Dec 21, 2013 by loick (4,970 points)
0 votes

Of course you can, but just for this problem. It is not a good style to change the source data in a project.

answered Feb 21 by knight3396 (140 points)

...