Interview 1: Find three occurrences of a number in an array

Source: Internet
Author: User

Version1: Find a number in an array that appears only once, and the other numbers appear two times: all numbers are different or the resulting result is only one occurrence. Version2: Find two occurrences of a number in an array, the other numbers appear two times: The result of all the numbers is X=a^b, the index is the first digit in X is 1, then A and B index is bound to have a 1, there is a 0. According to this, all the data are divided into two groups, the index of each group is 1, the other group index is 0, the first set of the number is different or get a, the second group of numbers XOR B. The time complexity is O (n) and the spatial complexity is O (1). (To determine whether a person is 1:int result=a& (1<<i)) If the result is not 0, then the first bit of a is 1; otherwise I is 1.  version3: Find three occurrences of a number in an array, and the other numbers appear two times: all numbers are different or the results are x=a^b^c. x must not be the same as a,b,c, so x^a,x^b,x^c does not define the function f (n) for 0. , the function is to preserve the last digit in the binary representation of the number N 1, while the other bits become 0. The results of F (x^a), F (x^b), F (x^c) are not 0. Consider the result of F (x^a) ^f (x^b) ^f (X^C), which is certainly not 0. Because for three nonzero number i,j,k, the result of f (i) ^f (j) is either 0, or two of the results of the binary result is 1, and the result of f (k) is only 1, so f (i) ^f (j) ^f (k) must not be 0.   therefore F (x^a) ^f (x^b) ^f (X^C) has at least one of 1, assuming that the last one is 1 bits for the first m. So X^a, x^b, x^c The result, there is one or three digits of the first m bit is 1. The following proves that it is not possible to have three results of the first m bits are 1. Contradiction proof: If x^a,x^b, the x^c of the first M is 1, then the M-bit of a,b,c is the opposite of the M-bit of x, then the a,b,c is the same as the first M bit. If all is 0, then the x=a^b^c of the M is also not 0, and the first m bit of x and the a,b,c m-bit opposite contradiction, similarly if all is 1, then x m is 1, and the hypothesis contradiction. So there is only one number in the a,b,c with the M bit of 1. So we found a standard that distinguishes a,b,c three numbers. Once the number of M-bit 1 is found, the other two numbers can be found.  version4: In an array, except one number appears once, the other numbers appear three times to find only one occurrence of the number. Idea: If there is no x in the array, all the other numbers appear three times, then all the numbersEach bit of the binary representation should be divisible by 3, and if one cannot, then X is 1 on this one. This solution can be extended to all other numbers that have occurred n times. Code://"Vernacular Classic Algorithm series 17" <span > array only one occurrence of the number </span>//by Morewindows (http://blog.csdn.net/MoreWindows)// Welcome to Http://weibo.com/morewindows#include <stdio.h> #include <string.h>int findnumber (int a[], int n) {int Bits[32];int I, j;//accumulate all the numbers in the array bits memset (bits, 0, 0 * sizeof (int)); for (i = 0; i < n; i++) for (j =; J <; J + +) BITS[J] + = ((A[i >> J) & 1);//If the result on a bit cannot be divisible, then the target number on this one is int result = 0;for (j = 0; J < All; J + +) if (Bits[j] % 3 = 0) Result + = (1 << j); return result; int main () {printf ("" "17" in the "Vernacular Classic Algorithm series");p rintf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n"); printf ("--http://blog.csdn.net/morewindows/article/details/12684497--\ n");  const int maxn = 10;int A[maxn] = { 2, 3, 1, 2, 3, 4, 1, 2, 3, 1};p rintf ("%d\n", Findnumber (A, MAXN)); return 0;}     

Face question 1: Find three occurrences of a number in an array

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.