Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to find a max element of highest sequence number

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading