I had to develop an algorithm that will find gap between sequential Ids within a List<long>
.
For example as input we had the following List<long> = {1,2,3,6,7,8,11,12,15,16}
. This means that algorithm should find the numbers 4,5,9,10,13,14
.
For this task I developed the following method, but not sure about effectiveness of this method in case of large data input, also about clean code readability.
private const int AllowedGap = 1;
public static List<long> FindMissingSequentialIDs(List<long> ids)
{
List<long> response = new List<long>();
for (int i = 0; i < ids.Count() - 1; i++)
{
if (ids[i + 1] - ids[i] > AllowedGap)
{
long currentGapCount = ids[i + 1] - ids[i] - 1;
long missedSeqId = 0;
while (currentGapCount != 0)
{
if (missedSeqId == 0)
{
missedSeqId = ids[i + 1] - 1;
response.Add(missedSeqId);
currentGapCount--;
if (currentGapCount == 0)
{
break;
}
}
else
{
missedSeqId = missedSeqId - 1;
response.Add(missedSeqId);
currentGapCount--;
}
}
}
}
return response;
}
Seems like this worked , but what would you improve here ?