Interview question: Given a sequential file containing 4.3 billion 32-bit certificates, find an integer that contains at least two times

Source: Internet
Author: User
Tags integer

Interview question: Given a sequential file containing 4.3 billion 32-bit integers, find an integer that contains at least two times.

Idea: Consider two conditions

1. All integers are stored in the sequential file, so the number of times the file is read will significantly affect the efficiency of the algorithm

2. The number of integers contained in the sequential file is 4300000000, and if all reads are placed in memory, the memory space factor must be considered.

So, there is no time-saving and space-saving solution. That is, try to read only one file at a time and use as little memory as possible.

Solution:

By the above question, we think of Bit-map, can apply 537.5 million char array, each bit in the array corresponds to a number of 4.3 billion integers, at the beginning, all the bits are 0, if there is a corresponding number, then the corresponding bit is set one.

The question comes out again, how can I represent an integer that contains at least two times.

This is, we find that it is not enough to represent an integer that contains at least two times, only one. So with two bits. 00 means there is no data, 01 means there is one, 10 means there are two, 11 means there are more than two, over.

We need to apply an array of type char of size 1075000000, with two bits corresponding to one number.

Initially, all bits are zeroed, and then the sequential file is read, and after the integer is read, the corresponding bit is changed accordingly.

In this way, we only need one operation, and the least amount of memory is used to solve this problem.

Isn't it easy?

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.