Bitwise operations Solve "one array, only one number appears n times, other numbers occur K-times" problem

Source: Internet
Author: User
Tags bitwise bitwise operators

Turn from: 52928864

After learning the bit operations, you will often encounter a class of questions about finding missing integers.
The first class is to give you an array that tells you what the range of these numbers is, and then lets you look for this missing number (for example, the range of unordered arrays is from 1 to 10, not repeating 9 numbers).
The solution to this type of problem is more diverse, the first, because given the range can be calculated by calculating the sum of the numbers, and then subtracting the numbers, leaving the missing numbers. The second is to sort the array, traverse the entire array, and then determine whether the adjacent elements are contiguous, and if so, the missing number is in two paragraphs, and if discontinuous, the missing is the one that needs to be found.

The second class still gives you an array, and then these numbers appear even several times, and only one number appears odd times, allowing you to find this odd number of numbers, for example (1, 2, 3, 4, 1, 2, 3).

Idea: By an operation, the same elements can be eliminated from each other after the operation, and the remaining elements are the ones to be searched. This bitwise operation is "XOR". By XOR, the same number of all bits is 0, and the individual number is itself.

When the condition of the topic changes, the number of the array may appear several times, but only one number appears once, looking for this number (assuming that it appears here 3 times).

Idea: for integers 32 bits, for each bit, the number of the entire array is combined to remove the remainder of 3, which is the value on this bit of the element.

Summary: When an array has a number that happens to be k-times, it can be solved with this method. Use the appropriate bitwise operators to save each bit, and then find the original number in this bit.

The third class will be more difficult to improve, or give you an array, the number is still in pairs appear, but the number of individual occurrences into two, find the two numbers.

Idea: It is possible to use bitwise operations to divide the array into two parts, each containing an integer that appears only once, so that the sub-topic is almost the same as the second class. The specific steps are to each element of the array to be different or to get a result of two number of different or, in this result contains at least one bits is 1.

Summary: Arbitrarily select a bits, then divide the array into two parts, the last of which is 1, and the other part is 0. Since these two separate numbers are definitely different, they will certainly not be in the same group after grouping. The solution to this problem is similar to the one above.

As can be seen from the above examples, finding missing numbers is primarily a bitwise operator to counteract the element that satisfies the condition

Bitwise operations Solve "one array, only one number appears n times, other numbers occur K-times" problem

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.