Searching for lost numbers for interview questions

Source: Internet
Author: User

Source of http://fayaa.com/tiku/view/2/

 

It is said that it names such as MS/Google have interview questions:

There is a group of numbers, ranging from 1 to n, which reduces the number and the order is also disrupted. They are placed in an n-1 array.

Please find the missing number. It is better to have a program, and the best algorithm is faster.

Btw1: there are many methods. It is said that there are more than one O (n) method.

Btw2: expansion problem. What if two digits are lost?

Btw3: Be careful not to overflow. Well, the interviewer will not remind you sometimes.

Btw4: it is best not to apply for n more space

Update

A very similar question: 1-1001 is placed in an array containing elements, and only one unique element value is repeated. Each array element can only be accessed once. design an algorithm to find it. Can you design an algorithm to implement it without supporting the storage space?

 

Question

:

  1. Given n numbers, only one of them has an odd number and the other has an even number. Use the linear time constant space to find the number that appears an odd number.
  2. Given n numbers, only two of them appear odd times, And the rest appear even times. Use the linear time constant space to find the two numbers with an odd number.

 

 

 

Here are some answers:

Question 1: the first response to the question is to add up the n-1 numbers and compare them with the sum of 1 + 2 + 3... + N. The difference is the lost number. However, this method is exactly as mentioned in btw3, so there is a certain risk, but it is still not used. Some people may say that it should be judged by one +, it can be, but it is difficult to write the code (personal feeling), or it is the most suitable for different or operators. We know that 1 ^ 1 = 0; 2 ^ 2 = 0; n ^ n = 0; k ^ 0 = K; so if we compare the n-1 numbers, let's try again or 1, 2 ,.. n. The final answer is K ^ (1 ^ 1) ^ (2 ^ 2)... ^ (N ^ N ). K. We can easily write down the function:

// In case find one missing number, here size is 1 less than the range n <br/> int find_missing_number1 (int A [], int size) <br/> {<br/> int number = 0; <br/> for (INT I = 0; I <size; I ++) <br/> Number ^ = (I + 1) ^ A [I]); <br/> Number ^ = (size + 1); <br/> return number; <br/>}

The second question is more complicated. What if two numbers are lost? It is still the method of method 1, but it needs to be derived. Assume that the number we lost is S1, and S2 is the value that S1 ^ S2 only has after all or excludes. You can find out after analysis, S1! = S2, that is, S1 ^ S2! = 0; in this case, the binary bit of S1 ^ S2 is 1, so we can divide all these numbers into two groups, and the binary bit of one group is 1, the other binary bit is 0 to redo the XOR. In this way, we can find one of the S1, and then get S1 ^ (S1 ^ S2. Similarly, we wrote the following code:

// In case find two missing numbers, here size is 2 less than the range n <br/> void find_missing_number2 (int A [], int size, Int & miss1, int & miss2) <br/>{< br/> miss1 = 0; <br/> miss2 = 0; <br/> int number = 0; <br/> for (INT I = 0; I <size; I ++) <br/> Number ^ = (I + 1) ^ A [I]); <br/> Number ^ = (size + 1); <br/> Number ^ = (size + 2 ); </P> <p> // now number will be miss1 ^ miss2 <br/> // find the binary 1 in number <br/> int K = Number-(number & (Number-1 )); <br/> for (INT I = 0; I <size; I ++) <br/>{< br/> If (I + 1) & K) <br/> miss1 ^ = (I + 1); <br/> if (a [I] & K) <br/> miss1 ^ = A [I]; <br/>}< br/> If (size + 1) & K) <br/> miss1 ^ = size + 1; <br/> If (size + 2) & K) <br/> miss1 ^ = size + 2; <br/> miss2 = Number ^ miss1; <br/>}

I also found a test case to test it:

Int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> int A [] = {, 3 }; <br/> int B [] = {1, 2, 4, 5, 9, 7, 6, 11, 3, 10}; <br/> int S1, S2; <br/> int K = find_missing_number1 (A, sizeof (a)/sizeof (A [0]); <br/> find_missing_number2 (B, sizeof (B) /sizeof (B [0]), S1, S2); <br/> STD: cout <"Missing Number 1 is" <k <STD: Endl; <br/> STD: cout <"missing number 2 is" <S1 <"and" <S2 <STD: Endl; <br/> system ("pause"); <br/> return 0; <br/>}< br/>

It seems that the conclusions of the two functions are correct.

The following question, update, is indeed mentioned above, is very similar. If we compare these numbers with 1-, we can get the answer. In fact, the first two questions of the odd number correspond to the Lost number and the two numbers respectively. The idea is completely the same.

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.