# Find the maximum number of consecutive natural numbers [longest consecutive sequence in an unsorted array]

Source: Internet
Author: User

Http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html

[Question]

An unordered natural number array, such as [, 2,], calculates the maximum number of consecutive natural numbers in the time complexity of 0 (n). The output value should be 3.

[Analysis]

Method 1: Sort
You can use some sorting methods such as base sorting, bucket sorting, and count sorting. Then you can traverse all the elements. There are some restrictions on the current sorting.

Method 2: maintain a hash table
Maintain a hash table with the maximum size integer. Traverse the array once and use the hash table to record the number of entries in the original array.
Then set four variables to indicate start, end, length, bestlength = 0. Initially, start = END = the first number in the array, length = 1. Then, perform the following operations:
End = end + 1. Then look for end in the hash table. If it can be found, it indicates that end exists in the original array. Until the end position is not found.
Set length = end-start. If the length is greater than bestlength, update: bestlength = length.
Set start and end to the number found just now, length = 1, and then repeat the above operation. The final bestlength is the maximum number of consecutive natural numbers.
Since hash search and other operations can be completed within the O (1) time complexity, the hash method can meet the O (n) time complexity.

Method 3: bitmap
Use bitmap. Method 2 is similar.
The bitmap size is related to the maximum integer. Each digit in the bitmap is 0 or 1. 1 indicates that the index appears in the original array. Otherwise, it does not exist. After traversing the original array to create a bitmap, traverse the Bitmap Using the method similar to method 2 to find the maximum number of consecutive natural numbers.
There is a problem with the bitmap method: the maximum number may be large, but the number is small. At this time, the space of the bitmap to be applied is still large, the complexity is not O (n ).

[Reference]

Http://stackoverflow.com/questions/7453248/longest-consecutive-sequence-in-an-unsorted-array

Http://stackoverflow.com/questions/5415305/finding-contiguous-ranges-in-arrays

Http://blog.csdn.net/xiajun07061225/article/details/8706276

Http://www.cnblogs.com/sooner/p/3279429.html

Find the maximum number of consecutive natural numbers [longest consecutive sequence in an unsorted array]

Related Keywords:

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.