A file contains 4 billion integers, each of which is 4 bytes and memory is 1 GB. Write an algorithm: Find an integer not included in the file

Source: Internet
Author: User

An integer expressed in four bytes. A total of only 2 ^ 32 is approximately 4G.
For the sake of simplicity, we can assume that they are all unsigned integers.
Allocate MB memory. Each bit represents an integer, which can represent an integer of 4 bytes. The initial value is 0. The basic idea is to set the corresponding bit location to 1 for each number read. After processing the 40 Gb number, traverse the memory of MB to find a bit of 0, the integer corresponding to the output is not displayed.
Algorithm flow:
1) Allocate MB of memory Buf and initialize it to 0
2) unsigned int x = 0x1;
For each Int J in file
Buf = Buf | x <J;
End
(3) For (unsigned int I = 0; I <= 0 xffffffff; I ++)
If (! (BUF & x <I ))
{
Output (I );
Break;
}
The above is only for unsigned, and signed integers can be pushed accordingly.

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.