I have an array of "Range" objects with properties "Offset" and "Length", as shown below. Assume that it'll be sorted by "Offset" ascending.
Range array contains:
Offset Length Index
------- ------- -------
100 10 0
110 2 1
112 5 2
117 3 3
300 5 4
305 5 5
400 5 6
405 10 7
415 2 8
417 4 9
421 7 10
428 1 11
429 6 12
500 4 13
504 9 14
The contiguous subsequences in this case would be:
Sequence #1 indices: 0, 1, 2, 3
Sequence #2 indices: 4, 5
Sequence #3 indices: 6, 7, 8, 9, 10, 11, 12 <-- (longest!!)
Sequence #4 indices: 13, 14
Assume that there will only be one longest sequence. Iterating through the items, i thought to create a new array for each contiguous sequence and return the biggest array, but that seem sub-optimal. Is there a better way to do this? I'm implementing in C# 2.0. The function should either return an array containing the elements of the longest subsequence, or the starting and ending indices of the longest subsequence in the original array.
Thanks everyone for taking a stab at this.