Find the missing quantity

Source: Internet
Author: User
Introduction to algorithms Chapter 4 questions 4-2:

An array a [1. N] contains all integers ranging from 0 to N, but one of them is no longer in the array. In this case, we cannot use a single operation to access a complete integer in A. the only operation we can use is to take the J-bits of a [I ", the time taken by this operation is a constant. An algorithm is designed to locate the missing integer in the O (n) time. Any complete integer other than a can be accessed by a single operation.


Idea: the topic of Chapter 4 is "recursive". This problem may be caused by the Division and control method, and the scale of the problem may be reduced by some means. The division symbol "/" used below is the computer integer division operator.


(1) For the percentile bits of All integers in A, if A is ordered, 0 and 1 are alternating. The total number of 0 and the total number of 1 are at most 1 different. 0 ~ can be determined based on the parity of N ~ The number of random digits 0 of N and the number of 1. Then, after performing a round of statistics on the percentile bit of a [1. N], we can see whether the percentile bit of the missing number is 0 or 1.

A) if n is an even number, then 0 ~ The number of random digits 0 of N is n/2 + 1, and the number of 1 is n/2. After a round of statistics, if the number of missing integers is 0, we only need to continue to investigate these array items with the number of missing integers: Ignore the number of missing integers, the problem is to find the number missing from 0 to n/2 in this subarray. If we find that the number is 1, similarly, as long as you continue to investigate the array items with the first digit: Ignore the second digit, the problem is converted to finding the number of missing values from 0 to n/2-1 in this subarray.

The recursion formula is T (n) = T (n/2) + N.


PS: If n is an even number, then 0 ~ N all the even numbers are divided by 2, and the obtained sequence is 0 ~ N/2; 0 ~ All the odd numbers in N are divided by 2, and the obtained sequence is 0 ~ N/2-1.

If n is an odd number,

Then 0 ~ N all the even numbers are divided by 2, and the obtained sequence is 0 ~ N/2; 0 ~ All the odd numbers in N are divided by 2, and the obtained sequence is 0 ~ N/2.

 

B) N is an odd number, similar to.

 

 

(2) consider the highest bit. Suppose K = [log2

N]-an integer that contains k + 1 digits. So 0 ~ The number of the highest bits in N is 2 K.

(0 ~

2 K

-1)

, The maximum number of 1 is the n-2k

.

After a round of statistics on the highest digit, we can conclude that the missing number is between 0 and ~ 2 K

-Within 1, or 2 K

~ N, if the former ignores the highest bit, the problem is converted to finding

0 ~ 2 K

-1;

If it is the latter, ignore the highest bit. The problem is to find 0 ~ in the sub-array ~ N-2k

.

 

 

Another question:

How can I find the number missing in a [1... n.

(1) to 0 ~ N is summed, and the sum of the data in a is subtracted.

(2) Use an exclusive or operation. 0 ^ 1 ^... ^ n ^ A [1] ^... ^ A [n] is the number of missing values.

(3) After the end of a fast sorting round, if Element B of the central axis is either B + 1 or B, the problem is converted to a [B + 2... search for B + 1 ~ in N ~ Number of missing values in N; if the latter, the problem is converted to a [1 ~ 0 ~ In the b-1 ~ Number of defects in the B-1.

 

 

 


 

 

 


 

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.