Returns the smallest integer.

Source: Internet
Author: User

Question: How to find the first integer greater than 0 and not in the unordered integer array. For example, [100, 0] returns 3, [,-], returns 2, [1, 5, 3, 4, 2], returns 6, [, 3, 2, 1, 6, 8, 5] returns 4. Requires O (1) space and O (n) Time

A: keep a [I] = I.

# Include <stdio. h> void swap (Int & A, Int & B) {int c = A; A = B; B = C;} int findfirstnumbernotexistenceinarray (int A [], int N) {int I; for (I = 0; I <n; I ++) while (A [I]> 0 & A [I] <= N & A [I]! = I + 1 & A [I]! = A [A [I]-1]) Swap (A [I], a [A [I]-1]); for (I = 0; I <N; I ++) if (a [I]! = I + 1) break; return I + 1;} void printfarray (int A [], int N) {for (INT I = 0; I <n; I ++) printf ("% d", a [I]); putchar ('\ n');} int main () {const int maxn = 5; int A [maxn] = {2,-100, 4, 1, 70}; printfarray (A, maxn ); printf ("the missing number of this array is % d \ n", findfirstnumbernotexistenceinarray (A, maxn); Return 0 ;}

Reference: http://blog.csdn.net/morewindows/article/details/12683723

Returns the smallest integer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.