In an integer array, except for one or two or three digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Required time complexity is O (n), spatial complexity is O (1)

Source: Internet
Author: User
Tags diff

Rough gave the analysis, recently tired, will be improved later. The topic includes three small questions, from simple to complex: 1, if only one appears once, to investigate the nature of the XOR, is if the same number and their own or the result of the work is zero, then loop through the array, the elements in the array to do all the different or operation, then two times the number is all different or dropped, The result is the number that appears only once. 2, if there are two only one occurrence of the number, set to a, B. It is also a result of applying XOR, but all the different elements of an array element are x=a^b, because a, B is not the same number, so X is definitely not 0. For x, starting from low to high, find that the first bit of bit 1 is set to the M-bit, the bit m bit must come from a or B, it is not possible at the same time, the M-bit (from low to high) of a B is 1. In this way, the array can be divided into two parts according to the M-bit, with a group of M bits of 0 and a group of 1 m bits. Thus, the problem is decomposed into a number that only appears once in two arrays. The following first gives this part of the program and example analysis: [CPP]View Plaincopy
  1. <span style="FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:14PX;" > #include <stdio.h>
  2. int get_first_position (int num)
  3. {//start from low to find the first bit bit 1
  4. int index=1;
  5. int i=0;
  6. While (i<32)
  7. {
  8. if ((num& (1<<i)) = = (1<<i))
  9. Break ;
  10. Else
  11. {
  12. index++;
  13. i++;
  14. }
  15. }
  16. return index;
  17. }
  18. int is_bit_one (int num,int index)
  19. {//Determines whether the bit bit at the given index position is 1
  20. Num= (Num>>index);
  21. return num&1;
  22. }
  23. void Get_two_unique_num (int *a,int n,int *num1,int *num2)
  24. {
  25. int exclusive_or_result=0;
  26. *num1=0;
  27. *num2=0;
  28. For (int i=0;i<n;i++)
  29. Exclusive_or_result^=a[i];
  30. int index=get_first_position (exclusive_or_result);
  31. For (i=0;i<n;i++)
  32. if (Is_bit_one (a[i],index))
  33. (*NUM1) ^=a[i];
  34. For (i=0;i<n;i++)
  35. if (!is_bit_one (a[i],index))
  36. (*num2) ^=a[i];
  37. }
  38. void Main ()
  39. {
  40. int a[]={2,2,4,4,6,6,3,5};
  41. int num1,num2;
  42. Get_two_unique_num (A,sizeof (a)/sizeof (int), &num1,&num2);
  43. printf ("%d\t%d\n", num1,num2);
  44. }</span>

3, considering that there are three separate numbers in a given array, this is slightly more complex than the two. Step analysis, set these three numbers to A,b,c: (1) The number in the array is all different or the result x=a^b^c, but X is not one of a,b,c, assuming x=a, then b^c=0 description b=c, with the topic given the conditions of contradiction. (2) Set F (n) can be as in 2, starting from the low, find the first bit 1 position, F (x^a), F (x^b), F (x^c) to get the value is certainly not 0, because X^a,x^b,x^c itself is not 0. F (x^a) ^f (x^b) ^f (x^c) result is not 0. Because the result of F (x^a) ^f (x^b) may be 0, there may be two bit 1. If you assume that the result of the bit 1 of f (x^c) is coincident with one of the F (x^a) ^f (x^b), then the result of f (x^a) ^f (x^b) ^f (X^C) is only 1 bit 1, and if not coincident then there are 3 bit bits 1. (3) This can infer that at least one bit bit in F (x^a) ^f (x^b) ^f (x^c) is 1. The Mbit bit from low to high is assumed to be 1. Then you can conclude that there is one or three of the x^a,x^b,x^c in the M-bit 1 (there is no two, because there are two, the outcome of the XOR is 0). (4) It is proved that only one m-bit bit in x^a,x^b,x^c is 1. Assuming that their m-bit is 1, then x is 0, but the x=a^b^c is definitely 1, so the hypothesis is not true. On the contrary, it is assumed that the 1,a,b,c m bit of x is 0 and is not true because of the x=a^b^c. So in summary x^a,x^b,x^c only one of the first m bit is 1. Then this is a good question. Find the first number that appears only once, according to the M-bit. Then the remaining two is the problem described in question 2. The following code is given: [CPP]View Plaincopy
  1. #include <stdio.h>
  2. int get_first_bit (int num)
  3. {
  4. return num&~ (num-1);
  5. }
  6. void Get_two_unique_num (int *a,int n,int *num1,int *num2)
  7. {
  8. int result_code=0;
  9. For (int i=0;i<n;i++)
  10. Result_code^=a[i];
  11. int diff=get_first_bit (result_code);
  12. *num1=0;
  13. *num2=0;
  14. For (i=0;i<n;i++)
  15. {
  16. if (A[i]&diff)
  17. {
  18. (*NUM1) ^=a[i];
  19. }
  20. Else
  21. {
  22. (*num2) ^=a[i];
  23. }
  24. }
  25. }
  26. void Get_three_unique_num (int *a,int n,int *num1,int *num2,int *num3)
  27. {
  28. int result_code=0;
  29. For (int i=0;i<n;i++)
  30. Result_code^=a[i];
  31. int flag=0;
  32. For (i=0;i<n;i++)
  33. Flag^=get_first_bit (Result_code^a[i]);
  34. Flag=get_first_bit (flag);
  35. *num1=0;
  36. For (i=0;i<n;i++)
  37. {
  38. if (Get_first_bit (Result_code^a[i]) ==flag)
  39. {
  40. (*NUM1) ^=a[i];
  41. }
  42. }
  43. For (i=0;i<n;i++)
  44. {
  45. if (a[i]== (*NUM1))
  46. {
  47. int temp=a[i];
  48. A[I]=A[N-1];
  49. A[n-1]=temp;
  50. Break ;
  51. }
  52. }
  53. Get_two_unique_num (A,N-1,NUM2,NUM3);
  54. }
  55. void Main ()
  56. {
  57. int a[]={2,2,4,4,6,6,3,5,7};
  58. int num1,num2,num3;
  59. Get_three_unique_num (A,sizeof (a)/sizeof (int), &NUM1,&NUM2,&NUM3);
  60. printf ("%d\t%d\t%d\n", num1,num2,num3);
  61. }

In an integer array, except for one or two or three digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Required time complexity is O (n), spatial complexity is O (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.