how to find the max sequence order number before missing element.
input – 123678 output -3
public static void Main()
{
List<int> nums = new List<int>(){1,2,3,6,7,8};
int count = nums.Count;
for(int i=0;i<count;i++){
if((nums[i+1]-nums[i])>1){
Console.WriteLine("Missed Element after digit :" [i]);
}
}
}
fiddle https://dotnetfiddle.net/Hkd0rx
error
Index was out of range. Must be non-negative and less than the size of
the collection.
>Solution :
You need to skip the last number because there’s nothing to compare it to after it. So in your for loop change the condition to loop while i<count-1 is true.
You also have an error in your WriteLine. Right now it will pick the character from the string Missed Element after digit : at a specific index. To properly concatenate strings you could use + or string interpolation.
List<int> nums = new List<int>() { 1, 2, 3, 6, 7, 8 };
int count = nums.Count;
for (int i = 0; i < count - 1; i++)
{
var current = nums[i];
var next = nums[i + 1];
if ((next - current) > 1)
{
Console.WriteLine("Missing Element(s) between : " + current + " and " + next);
}
}