Introduction to algorithms-4-2 find the missing integer

Source: Internet
Author: User

[Switch]

Problem:
An array a [1 .. n] contains all from 0 .. all integers of N, but one of them is not in the array .. n] to record the integers in.
O (n) Time to find the missing integer. However, in this case, we cannot use a single operation to access a complete integer in A, because the elements in a are represented in binary. The only operation we can use
The operation is to take the J-bits of a [I] and the time taken is constant.

Proof: if the only way to access the information in array A Is this single bit operation, the missing integer can still be found in O (n) time. Any complete integer other than a can still be accessed by a single operation. [Calculation
Method Introduction Chinese Version P50 4-2]
I saw this question in the introduction to algorithms last night. I didn't think about it for a long time. What I was thinking at the time was to obtain every piece of information in a one by one, but the complexity was O (lgn). Today I learned this method by reading others' blogs on the Internet. The binary feature is used. (The following content is modified in the Jianxing blog. Address http://gonewiththedream.spaces.live.com/blog/cns! 4327b97ac534d77e! 235. Entry)

The basic method is to use the bipartite method: 1, traverse the first digit of an integer 0 to N and divide it into two Arrays: P1 [1] and P0 [1], representing the number of the first digit 1 and 0, respectively, record the number countn with the first value being 1 at the cost of O (n)
2. Traverse array a [1... n] First, divided into two groups: Q1 [1] and q0 [1], representing the number of 1, 0 respectively, and recording the number of 1 counta, price is O (n)
3. Compare the values of countn and counta. There may be two possible results: countn = counta, or countn = counta + 1. The former indicates that the first digit of the missing number is 0, and the latter is 1, cost: O (1)
4,
Through the result of 3, we can then go to P1 [1] and Q1 [1] (countn> counta, that is, the number with the first digit being 1 is missing) or
P0 [1] and q0 [1] (countn = counta, that is, the 2nd bits in the first bits are missing) Repeat the operations in step 1, 2, record arrays P1 [2], P0 [2], and
Countn 'and Q1 [2], q0 [2], and counta '. The cost is O (n/2) and O (n/2 ),
After comparison, we can see whether the second digit of the missing number is 0 or 1. Then we can compare P1 [2] and Q1 [2] Or P0 [2] and q0 [2]. price O (1)
5. Repeat the ceiling (lg (n) times. Finally, we can find that the total cost of the missing number is 2 * (O (n) + O (n/2) +... + O (N/POW (2, K) +... + O (1) = 2 * O (2n) = 4 * O (n) = O (N)

Of course, a problem is ignored here. If one of the missing numbers in a is the n-1 number, what is the extra number? If there are duplicates with other numbers, the above method is ineffective and the situation becomes quite complicated. Therefore, the above only applies to the case where the extra number is 0, or simply only the n-1 number.

[Other]

Weak method: sum of all numbers from 1 to n minus the sum of numbers from a [1] to a [n]

Sharp method: Merge M, M = 4k-1, and it is the maximum integer of 4 k-1> = N (for example, n = 7, M = 7; n = 16, then M = 19; n = 21, M = 23, and so on)
Then you only need a [1] ^ A [2] ^ ...... ^ A [n-1] ^ A [n] ^ {A [m-2] ^ A [M-1] ^ A [m]}, you can

Cause: four consecutive numbers are exclusive or starting from an integer multiple of 4. The result is (for example, the result of 4 ^ 5 ^ 6 ^ 7 is 0; 204 ^ 205 ^ 206 = 207; 8 ^ 10 ^ 11 = 9)
So 0 ^ 1 ^ 2 ^ ...... ^ M returns 0, but which number is used? A [1] ^ A [2] ^ ...... ^ A [n-1] ^ A [n] ^ {A [m-2] ^ A [M-1] ^ A [m]} is the missing number

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.