A logical question-the number I took

Source: Internet
Author: User

There are 10000 to 10000 numbers in total. If I randomly take a number from it, how do you know which one I took?

I believe many people have read this question and know the answer. I have heard this question during chats with my colleagues over the past few days. I may as well record it as I have thought about it. It is actually a logical question.AlgorithmMy colleague first talked about his thinking process during the interview:

    1. Multiply the number of 10000, and then multiply the number of 9999 after a number.
      This algorithm is correct, but there are two potential problems:

      • If so many numbers are multiplied, the range will inevitably exceed the Data Type support provided by the system. Of course, you can implement your own algorithm for representing large numbers, but such performance will inevitably be affected.
      • Assume that if there is 0 in the provided array, multiplication is not available.
    2. In response to the questions raised above, the colleague thought of using addition, first finding the sum of 10000 numbers, and then subtracting the sum of 9999 numbers.
      In this way, the data will not overflow, and the addition efficiency is much higher than multiplication. Even if the data contains 0, there is no problem.
Then I passed the test. After I went back, I thought it was still scalable. Assuming that all the numbers will still overflow, what should I do, for example, from 1 to (2 ^ 64-1), I thought of a bit operation, which is the most amazing thing to know when the number is different from, or, or: first, all the numbers are exclusive, and then the numbers after a number are taken are exclusive. The two results are then exclusive or, that is, the number that is taken away.

I will use the numbers A, B, C, and D4 for demonstration, because the XOR or compliance with the combination Law and the exchange law (you can try it with 0, 1), so:

A ^ B ^ C ^ d = (a ^ B ^ c) ^ d
D = (a ^ B ^ C ^ d) ^ (a ^ B ^ C)

The benefit of exclusive or is that

    • No Overflow
    • The speed of XOR is faster than that of addition.
Expand the question. Is there a problem if it is not an integer but a floating point number? Of course not, because the operation is at the bit level, whether it is an integer or a floating point number, In this algorithm, it seems to be a bunch of BITs, there is no difference in processing.

Expand the question. If the provided number itself is beyond the representation range of the built-in type, for example, between 1 and 2 ^ 128, what should I do? This question is written in this article.ArticleIn the process of thinking, there is no good way for now.

(Exclusive or is indeed a magical operation. Later I plan to write a series of articles on memory-transaction management, and I will also mention the use of this magical exclusive or to implement the Undo-Redo algorithm .)

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.