A Baidu interview question

Source: Internet
Author: User

Problem: In a large array, two numbers appear only once, and the other numbers appear twice.

 

Principle: When two identical numbers are exclusive or, the result is 0. When two different numbers are acute exclusive or, the result is not 0.

Example: 99 ^ 99 = 1100011b ^ 1100011b = 0

95 ^ 99 = 1011111b ^ 1100011b = 0111100b = 60

 

In the large array given by the question, except for the two different numbers to be searched, all other numbers appear in pairs, according to the two identical numbers mentioned above or the result is 0, if the elements in the entire array are exclusive or, the result is the difference or result of the two unpaired numbers. Assume that two different numbers in the array are 95 ^ 99, and the XOR binary result is 0111100b, where four digits are 1, this indicates that the binary values of the two numbers are different from those of the four digits. (From the right to the left) They are 3rd, 4, 5, and 6, so we

Only the elements whose 6th bits are 1 and 0111100b in the array need to be different or,

Or, the elements whose 5th bits are 1 and 0111100b in the array are different or,

Or, the elements whose 4th bits are 1 and 0111100b in the array are different or,

Alternatively, the elements with 3rd bits as 1 in the array and 0111100b are exclusive or.

You can obtain a number from the two numbers you want. The last rule above is used as an example to describe the following: the number with 3rd bits as 1 in the array element, except for the two numbers you want, they all appear in pairs, and the result of their variance or is certainly 0. The two numbers that you want have only one 3rd-digit 1. Assume that the number is a (unknown at this time), and the other number is B (unknown at this time ), obviously, if a (unknown at this time) is different from 0111100b, or B can be obtained (known at this time), then 0111100b and the obtained B (known at this time) are used) perform the XOR operation to obtain a (which is known at this time ). For example, if the 3rd bits of 95 above are 1, 95 ^ 0111100b is used.
= 1011111b ^ 0111100b = 1100011b = 99, and then 99 ^ 0111100b = 1100011b ^ 0111100b = 1011111b = 95.

 

With the above solution, the code is easy to write. The reference code is as follows:

# Include <iostream> using namespace STD; void get_2_numbers (int * arr, int size, Int & number1, Int & number2) {// int Pos = 0 where the first bit from right to left is 1; // int exclusive_or = 0; // obtain the variance of each element in the array or for (INT I = 0; I <size; ++ I) {exclusive_or ^ = * (ARR + I );} number1 = exclusive_or; // temporarily Save the exception or result/* While (exclusive_or & 1) = 0) // If the rightmost bit is 0, the loop {++ Pos; exclusive_or = exclusive_or> 1;} // when the loop ends, the POS is displayed as the number from right to left in the XOR or result, position of the first BIT with 1 */Pos = exclusive_or & (-exclusive_or); // use this line of code to replace the above while loop exclusive_or = number1 according to kingbigeast's suggestion; for (INT I = 0; I <size; ++ I) {If (* (ARR + I)> POS) & 1) {number1 ^ = * (ARR + I) ;}} number2 = number1 ^ exclusive_or; return ;} int main (INT argc, char **) {// two numbers int A, B; // The following array simulates the large array, there are two different elements 95, 99, all other elements are paired with int arr [12] = {,}; get_2_numbers (ARR, sizeof (ARR)/sizeof (INT ), a, B); the two numbers required by cout <"are:" <Endl; cout <A <"\ t" <B <Endl; return 0 ;}

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.