The number of only one occurrence in the array of "offer of Swords" (1)

Source: Internet
Author: User

Reprint Please specify source:http://blog.csdn.net/ns_code/article/details/27649027


Topic Description Narration:
An integer array with the exception of two digits. The rest of the numbers have appeared two times.

Please tap the code to find the two numbers that appear only once.

Input:
Each test case consists of two lines: the first line includes an integer n, which indicates the size of the array. 2<=n <= 10^6. The second line consists of n integers. Represents an array element. The elements are int.
Output:
each test case accordingly. Only two occurrences of a single occurrence in the output array. The output of the numbers from small to large order.
Example input:
82 4 3 6 3 2 5 5
Example output:
4 6

Idea: The principle of xor or de-weight has been learned in the previous blog post, and it is known that there is only one number that only appears once in a way. But here are two numbers that only appear once, and we'll try to divide him into two sub-arrays, each of which includes a number that appears only once. The rest of the numbers have appeared two times. The idea of a sword on offer is very ingenious, still different from beginning to end or all figures. The result is actually the result of two different numbers that only occur once. In the result of XOR we find the rightmost bit in the second binary, which is 1, since it is 1. Note that the two digits of the XOR corresponding to this bit are certainly different. Inevitably one is 1, one is 0, so we can consider whether this bit is the first to divide the two sub-arrays, so that two of the only occurrences of the number is separated. But we also want to ensure that the number of two occurrences of the same sub-array, must not be two repeated numbers in two different sub-arrays, so that the result is not correct. It is very obvious that the same number of digits on the same bit is the same, either 1, or 0, so we can also infer if the bit is one that divides the numbers that appear two times into the same sub-array, which is assumed to be 1. In a sub-array, assuming 0, it is divided into a sub-array.

This ensures that only one occurrence of the number is present in each of the sub-arrays. The other numbers appear two times, each of which can be obtained by each of these two only occurrences. The time complexity is O (n).

Other than that. When all the elements are different or later, when you find the rightmost 1, I use a more concise code than the sword point. The main use of the following conclusions:

The number that is obtained after a number x,x& (-X) is reserved for the rightmost 1 in X. The other bits are all 0. Note that the-X here is the opposite number of ×,-x=~x+1, where the ~x means to reverse the X-all, do not confuse the two.

The following is the code for AC:

 #include <stdio.h> #include <stdbool.h>/* returns the lowest bit of num 1, everyone else is 0*/int FindFirstBit1 (int num) {///both and after the resulting number, the rightmost 1 of NUM is retained, and all other bits are placed in order to 0return Num & (-num);} /* Infer whether the specific bit in data is 1, where the specific bit to infer is determined by Res. Only one of the res is 1. The other bits are 0, returned by the FINDFIRSTBIT1 function, and the bits to be inferred in data are the bits */bool IsBit1 (int data,int res) {return (data&res) ==0) of the only 1 in the Res. False:true;} void findnumsappearonce (int *arr,int len,int *num1,int *num2) {if (Arr==null | | len<2) return;int i;int AllXOR = 0;//All XOR for (i=0;i<len;i++) allxor ^= arr[i];int res = FindFirstBit1 (allxor); *num1 = *num2 = 0;for (i=0;i<len;i++) {if (IsBit1 (Arr[i],res)) *num1 ^= arr[i];else*num2 ^= arr[i];}} int main () {static int arr[1000000];int n;while (scanf ("%d", &n)! = EOF) {int i;for (i=0;i<n;i++) scanf ("%d", arr+i); int num1,num2; Findnumsappearonce (ARR,N,&NUM1,&NUM2); if (Num1 < num2) printf ("%d%d\n", num1,num2); elseprintf ("%d%d\n", NUM2,NUM1);} return 0;} 
/************************************************************** &NBSP;&NBSP;&NBSP;&NBSP; problem:1351 &NBSP;&NBSP;&NBSP;&NBSP; user:mmc_maodun &NBSP;&NBSP;&NBSP;&NBSP; language:c &NBSP;&NBSP;&NBSP;&NBSP; result:accepted &NBSP;&NBSP;&NBSP;&NBSP; time:780 Ms &NBSP;&NBSP;&NBSP;&NBSP; memory:4820 KB ****************************************************************/


The number of only one occurrence in the array of "offer of Swords" (1)

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.