The smallest integer that does not appear.

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/ju136/article/details/8153274

Question:

# Interview question # How can I find the first unordered integer array that is greater than 0 and not the smallest integer in this array. For example, [, 0] returns 3, [,-] returns 2. It is recommended that the O (1) space and O (n) time be used.

Source: http://weibo.com/lirenchen.

 

Solution:

It is difficult to directly consider this issue. It is better to change the problem first.

 

Question 1

Given an array, the length is N. All elements except a [0] are a [I] = I. Find the first integer that is greater than 0 and is not the smallest integer in this array.

The answer is: N + (A [0] = N );

 

Question 2

Given an array, the length is N, and the elements in several locations meet the requirements of a [x] <0 or a [x]> N, all the remaining elements meet a [I] = I. Find the first integer that is greater than 0 and is not the smallest integer in this array.

A:

Obviously: when the value of X is limited to 0. It becomes Question 1.

In addition, the solution to this problem is easy to obtain.

Scenario 1: start from 1 ~ N starts scanning. When I is found! = A [I], I is returned directly. At this time, I must be the smallest integer that does not appear.

Case 2: After scanning ~ After N, it will return to problem 1.

1 For(I =1; I <n; ++I ){2If(I! = A [I])ReturnI;3 }4 Return(N + (n = [0]);

 

Question 3

After question 2 is given, you need to consider how to convert the original question to question 2.

In fact, the task to be completed becomes, how to put elements in an arrayYuanshen Homing. That is, let a [I] = I. If we can let these element gods return to the throne, then it will become question 2. It is easy to solve.

AlgorithmAs follows:

We scan from the back to the front, I = n-1 to 0;

Step 1 if a [I] <0 | A [I]> N is found, continue;

Step 2: If a [I] = A [A [I] is found, continue;

Step 3 if 0 is found <A [I] <n

It indicates that a [I] is needed to return to the throne. That is, put it on a [A [I.

Swap (A [I], a [A [I]).

Then jump to step 2.

 

Note: Pay attention to a case where there are duplicates, such as a [11] = 2, a [2] = 2. At this time, there is no need to exchange. Directly process the next element.

You can giveCode:

 1       Int I = N, T, temp;  2       If (! A | n <= 0 ) Return - 1  ;  3  4       While (-- I)> = 0  ){  5           While ( 0 <A [I] & A [I] <n & I! = A [I]) {  6 T = A [I];  7               If (A [I] = A [T]) Break  ;  8 Temp = A [I];  9 A [I] = A [T];  10 A [T] = Temp;  11   }  12 }

Okay, so far, the original question has become very simple. The code can be provided directly.

Original answer

 1   Int Find ( Int *, Int N ){  2       Int I = N, T, temp;  3       If (! A | n <= 0 ) Return - 1  ;  4   5       While (-- I)> = 0  ){  6          While ( 0 <A [I] & A [I] <n & I! = A [I]) {  7 T = A [I];  8               If (A [I] = A [T]) Break  ;  9 Temp = A [I];  10 A [I] = A [T];  11 A [T] = Temp;  12   }  13   }  14   15       For (I = 1 ; I <n; ++ I)  16           If (A [I]! = I) Return  I;  17   18      Return (N + ([ 0 ] = N ));  19 }

 

Algorithm complexity analysis

The complexity of the algorithm is analyzed here. The complexity of the algorithm is shown in the following code. MM ~~ It is hard to tell exactly what the complexity is.

We may wish to return to the problem itself. Analyze the following conditions:

Condition 1: for a given array. There is a limited number of returned gods. Assume K. This K indicates the number of a [I] = I after the processing is complete.

The range of K is fixed: 0 <= k <n.

Condition 2: For each element, while (0 <A [I] & A [I] <n & I! = A [I]) in this WHILE LOOP, every exchange will result in the return of a mono. If Xi is exchanged, the XI gods will be reset.

This is easy to understand. Because the effect of each exchange is to put a certain God in place.

For example, a [11] = 5, a [5] = 2, a [2] = 0;

First exchange: A [11] = 2, a [5] = 5, a [2] = 0;

Second exchange: A [11] = 0, a [5] = 5, a [2] = 2;

Condition 3: For the entire array, K must meet the following requirements:

X0 + X1 + X2 + X3 +... + xn-1 = K

That is to say, K is exchanged.

Note: Not every I will exchange K times. But the number of I exchangesSumIt should be K times.

 

You can imagine an extreme situation. For example, when I = n-1, the replacement times are the most. Just now, all the gods are taken back. From I = N-2InsideThe while loop is in progress.

For example, if I = n-1, the two gods are placed in place. So from I = n-2 ~ 0, you only need to let the K-2 a God back. Because it has been reset, you do not need to process it again.

 

Think of this, you should understand,The complexity of the code below is O (n) + O (k ). Because 0 <= k <n. Therefore, the complexity is O (n ). That is, the linear time completes the task..

 

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.