[Two numbers of odd numbers appear in the array] pen exam Selection

Source: Internet
Author: User

From: http://blog.csdn.net/wodewe/article/details/6863753 #

 

Question: There are n + 2 numbers, N numbers appear even, and 2 numbers appear odd (these two numbers are not equal). The space complexity of O (1) is used, find the two numbers. You do not need to know the specific position. You only need to know the two values.
Solution: if there is only one number that has exceeded an odd number, it is better to solve the problem. The elements in the array are exclusive or
The number that appears only an odd number of times.
But there are two odd numbers in the question ?, The solution is as follows:
Assume that these two numbers are a and B, and all elements in the array are exclusive or the result x = a ^ B is used to determine the number of digits where X is 1 (Note: because! = B, So X! = 0. We only need to know the K of a single digit as 1, for example, 0010 1100. We can choose K = 2, 3, or 5 ), then, the number where the K-bit of X and the array is 1 is exclusive or, the difference or result is one of A and B, and then the other can be obtained using X exclusive or.
Why? Because the k-th digit in X is 1, it indicates that the k-th digit of A or B is also 1. Assume that it is, when we convert X to the number where the K bit in the array is 1, that is, the number of times that X has exceeded the number of times that X and a plus other K-bit values are exclusive or. Simplification means that X and a are exclusive OR, and the result is B.

 

void getNum(int a[],int length) {     int s=0;//保存异或结果     for(int i=0;i<length;i++)     {         s=s^a[i];     }    int temp1=s;//临时保存异或结果    int temp2=s;//临时保存异或结果     int k=0;     while(!(temp1&1))//求位为1的位数     {         temp1=temp1>>1;         k++;     }     for(int i=0;i<length;i++)     {         if((a[i]>>k)&1)//将s与数组中第k位为1的数异或         {             cout<<a[i]<<" ";             s=s^a[i];         }     }     cout<<s<<" "<<(s^temp2)<<endl;//(s^temp2)用来求另外一个数 } 

 

 

[Two numbers of odd numbers appear in the array] pen exam Selection

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.