The 1th chapter of the game-quickly find fault machine

Source: Internet
Author: User

Reprint: The beauty of programming _1.5_ quickly find machine faults

Title: Suppose a machine only stores a record labeled ID, assuming that each piece of data holds 2 backups, so that 2 machines store the same data. Where the ID is an integer less than 1 billion

Question 1, at some time, if you get a list of data file IDs. Is it possible to quickly find the ID that appears only once in this table? That is, quickly find out the data ID of the failed machine store.

2. If there are two machines in the crash? (assuming that two backups of the same data are not lost at the same time, that is, the list is missing two different IDs)

Extended problem, if all the machines have three backups, that is, the same ID has three machines. And at the same time there are three machine crashes, still can use the above method to solve?

If there are n backup, and also have n machine crashes?

~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~

The question is re-elaborated:

Question 1, an array is known, only one of the data in the array appears once, the other data is two times, we need to find this data

Question 2, an array is known, there are two different data in the array to appear again, the other data is two times, we have to find out the two data

Problem 3, an array is known, the array is missing three data, we have to find out the three data

can then be extended to N

~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~

Question 1, an array is known, only one of the data in the array appears once, the other data is two times, we need to find this data

Method One, use count sort (with map)

Thought: Traverse the entire list of IDs and use map to record the number of occurrences of each ID. After that, only one occurrence of the ID is asked

Time complexity O (n), spatial complexity O (n)

Note: Instead of using a map, the spatial complexity will be greater than N (n is the number of N) and should be 1 billion (the maximum value of the ID may be 1 billion).

Cons: space complexity is too large, the ID has been two times have to save, but it is impossible to be a faulty machine

Method Two, still use the count sort, but the ID that has already appeared twice is not stored

thought: traverse the list, for each ID, first check whether there is the same ID in the hash table

If so, the ID is removed from the hash table ;

Otherwise, add the ID to the hash table.

Thus, after traversing the list, theremaining element in the hash table is the ID of the request .

Time complexity O (n), the space complexity is preferably O (1), the worst is O (n)

Method three, using XOR or operation (recommended)

thought: all the IDs in the list are different or, after that, the obtained value is the ID you are seeking .

Using the XOR operation can get

X^x=0 x^y=z x^0=x

x⊕x = 0   x⊕y = Z  x⊕0 = X  2 1 2 3 1 binary for 010,1 with binary for 001 3 for ID 2  3  for 011 2⊕< /c8>1 = 010⊕001= 011   011⊕2 = 011⊕010=001=1 (2⊕1⊕2 = 1)  1⊕3 = 001⊕011=010  010⊕001=011 = 3
      The final result  is still the number that only appears once.

The time complexity is O (N) and the spatial complexity is O (1). In time and space, the basic has reached the optimal.

disadvantage: The premise is that only one ID appears once, if there are multiple times, it is not suitable for

Method four, using "invariants" (recommended)

idea: here, the sum of all the IDs is a invariants, and now the remaining ID is summed. The difference between the sum of all IDs and the remaining IDs is the ID you are seeking.

Time complexity: O (N) time, Space complexity O (1)

Summary: Using XOR and invariants has been optimized. Can be resolved only once in a single list, with only one variable.

~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~

Question 2, an array is known, there are two different data in the array appears again, the other data is two times, we have to find the two data in the problem is lost is two different data, both of us here are considered

If the missing two numbers are not the same,

Method: Make a different or action

idea: due to the number of missing differences, the result of the final XOR is not 0.

(1) All IDs in an array are different or, as a  result (2) We find the binary representation of a, where the lowest one is 1, b  (3) is based on whether B bit is 1, the number in the ID array is divided into two arrays, where B in one array is 1, and B in the other queue is 0.  (note that in each array, except for the one that only appears once, the other number appears two times, so you can use XOR within the array)  (4) then the two arrays, respectively, will be different or operation, then get two not 0 of the number. Is the two ID that was lost.  

If the missing two numbers are the same

(All IDs in the array are paired, with an XOR value of 0, not an XOR implementation)

Method: You can use invariant implementations. Two lost, two equations generated, evaluated in a joint

At this point we take the following approach:

(1) first calculates the sum of all IDs before the initial loss.  (2) then calculates the sum of the IDs after the loss, then (1) (2) The result is subtracted operation, obtains the equation x+ y = A.  (3) using the difference between the square sum of the lost and before, and (2) to the joint, get the equation x * x + y * y = b.  (4) The two equations can be linked to the final result.

~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~

Question 3, an array is known, the array is missing three data, we have to find out the three data, then can be extended to n

Method One: We need to create three/n equations to find out which are the numbers

At this point, when the equation is n, it is not good to ask N equations

Method Two: Use the Count sort + count value to reach a when map is not stored

At this point, we can finally get these few numbers

~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~

Related Topics

Give you a clutter of poker (not including the size of the King), arbitrarily draw a card from it, how to use the simplest way to know that the 1~13 is the one? (not required to know the color)

Method: Using invariants

Pre-calculated all the cards and (1+...+13) x 4 = 364

Then subtract the remaining card points, and finally get the one that was pulled out.

The 1th chapter of the game-quickly find fault machine

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.