Example of a different or a heavy

Source: Internet
Author: User

This article does not have the code, introduces is the pure theory idea.

XOR or a binary bitwise operation, represented by the symbol XOR or ^, whose algorithm is to bits each of the two sides of the operator, with a value of 0, and an XOR value of 1. It differs from Boolean operations in that when both sides of the operator are 1 o'clock, the result of the Boolean operation is 1, and the result of the XOR operation is 0.

The nature of an XOR:

1, Exchange law: a^b = b^a;

2, Binding Law: (a^b) ^c = a^ (b^c);

3, for arbitrary a:a^a=0,a^0=a,a^ ( -1) =~a.

With this in view, it's important to take a look at this, and the following procedure will use this conclusion:

For any of a, there is A^b^c^d^a^k = b^c^d^k^ (a^a) = B^c^d^k^0 = B^c^d^k, that is, if there is a number of differences or, if there are duplicates, the number of duplicates, whether or not they are adjacent, can be eliminated by the different or the nature of these repeated numbers, In particular, if you repeat the occasional number of times, the different or later will all disappear, if the repeated occurrence of odd several times, the different or later will retain one.

Here's a look at two topics:

1 and 1-1000 are placed in an array of 1001 elements, and only one element value is duplicated, and all others appear only once. Each array element can only be accessed once, design an algorithm, find it out, do not use the auxiliary storage space, can you design an algorithm implementation?

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Of course, this problem can be done in the most intuitive way, add up all the numbers, minus the 1+2+3+...+1000, and get the duplicate number, which is easy to understand and efficient, and does not require auxiliary space, and the only disadvantage, if the range is not 1000, but a larger number, Overflow may occur.

We consider using a different or an operation to solve the problem. Now the question is the number that requires repetition, let's assume that the digital n bar, if we can find a way to 1-1000 in addition to the number of other than N or two times, and the number n only different or once, you can remove all the numbers in 1-1000 out of N, so that only N. We first put all the numbers in different or, as T, we can get the following:

T = 1^2^3^4...^n...^n...^1000 = 1^2^3...^1000 (the result does not contain n)

Then we can get the duplicate number n with all the numbers (containing only one n) between T and 1-1000. As shown below:

t^ (a^2^3^4...^n...^1000) = t^ (t^n) = 0^n = n

This problem ends here.

2, an array of only one number appears once, the other all appear two times, to find this number.

Understand the derivation of the above topic, this is very easy, all the elements of the array will be different or, the last occurrence of two of the elements will all be eliminated, and finally get the only one occurrence of the number.

This topic can also be the same as the following scenario, the idea is the same: only one number in the array appears odd several times, the others appear even several times.

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.