longest sequence of consecutive numbers I am trying to print longest consecutive element in sorted array  for that I have written following brute force algorithm 

void Main()
{
    var arr = new[] { 1, 2, 4, 5, 6 ,7,8,10,11,12};

    int max = 0;
    int count = 1;
    int startIndex = 0;
    int start=0;
    int endIndex=0;
    for (int i = 0; i &lt; arr.Length - 2; i++)
    {
        startIndex = i;
        while (i&lt;arr.Length-1 &amp;&amp; Math.Abs(arr[i] - arr[i + 1]) == 1)
        {
            i++;
            count++;
        }
        if (max &lt; count)
        {
            max=count;
            start=startIndex;
            endIndex=i;
            count=1;

        }



    }
    for (int k = start; k &lt;= endIndex; k++)
    {
        Console.WriteLine(arr[k]);

    }
}


I need some review comments on my solution( I don't want optimize solution)
I'm assuming in real production the values aren't hard coded and will be passed in.  Then you will want to make some guard clauses.  What is the array is empty or value of one? 

You also have more variables than you need, which isn't so bad just still if you know the start and the length of the run you don't need end position.  You can also move startofrun inside the loop.  Also better naming of variable would help. 

You don't need to do math.abs when dealing with ints. Just add one and see if the next one is the same value.   

Also while it's legal in C# to increase the for loop counter it's not intuitive to someone coming after you.  I would recommend both of them being while loops since the control variable is changing in the loop

For a reference a method like this.  I took out the printing as that's not what this method is intended for

static IEnumerable&lt;int&gt; LongestStreak(params int[] values)
{
    // guard clauses
    if (values == null)
    {
        return Enumerable.Empty&lt;int&gt;();
    }

    // If zero or one then return it right away
    if (values.Length &lt;= 1)
    {
        return values;
    }

    int currentPosition = 0;
    int currentLongestRun = 0;
    int startOfLongestRun = 0;
    while (currentPosition &lt; values.Length - 2)
    {
        int startOfRun = currentPosition;
        while (currentPosition &lt; values.Length - 1 &amp;&amp;
               values[currentPosition] + 1 == values[currentPosition + 1])
        {
            currentPosition++;
        }

        if (currentPosition - startOfRun &gt; currentLongestRun)
        {
            startOfLongestRun = startOfRun;
            currentLongestRun = currentPosition - startOfRun;
        }
        // move to check next sequence
        currentPosition++;
    }

    // C# zero based array so need to add one to the streak
    int[] result = new int[currentLongestRun + 1];
    Array.Copy(values, startOfLongestRun, result, 0, currentLongestRun + 1);
    return result;

}