There is one integer array and you must find out one subset that contains the maximum continuous numbers with time complexity O(n).
E.g. imput-> 15, 7, 12, 6, 14, 13, 9, 11
output-> 5: [11, 12, 13, 14, 15]
Resolve
One intuitive way is to use Disjoint Set which is a structure for unioning or searching element in set. The time complexity is O(n).
The second way is to maintain the information of continuous numbers set using a starting number and an offset. See code logic for more details.
The simplest way is to use bitset to count the numbers and then find out the longest set. Bitset is good at saving memory although it may be hard to define the length sometimes in this question.
Code
the 1st algorithm
the 2nd algorithm
the 3th algorithm
Notice: this article use BY-NC-SA license. Please specify the source: Awarrior