C Program to find length of longest consecutive elements sequence from given unsorted array of integers

In this blog post, we learn how to write a C program to find the length of the longest consecutive elements sequence from a given unsorted array of integers? So here we will write the C program to find the longest consecutive elements sequence in an unsorted array. We will also see how to display the sequence of consecutive elements in a sorted array using C programming.

Example,

//Given an input array
Input: arr[] = {2,5,7,7,8,8,9,4,10,12,3,6};

Output: 9

Explanation: The subsequence 2, 3, 4, 5, 6, 7, 8, 9, 10 is the longest subsequence of consecutive elements

Logic to find the length of the longest consecutive elements sequence

Note: Array should not be empty.

So let’s see the Logic to find the first and last position of the element in a sorted array. Suppose arr is a given sorted integer array of size N (arr[N] ), the task is to write the C program to find the length of the longest consecutive elements sequence from a given unsorted array of integers.

1. Create two intermediate variables length and longestConsecutiveSeq.

2. Initialize the length and longestConsecutiveSeq variables with 1.

3. Now sort the unsorted array using the qsort. If you want you can create your own function for sorting the array elements.

4.  Now iterate the sorted array elements from the beginning to end and skip the equal consecutive numbers.

5.  If the current element is equal to the previous element+1 then increase the length else set the length to 1.

if (arr[i] + 1 == arr[i + 1])
{
length++;
}
else
{
length = 1;
}

If you want to learn more about the C language, you can check this course, Free Trial Available.

C Program to find length of longest consecutive elements sequence from given unsorted array of integers

#include <stdio.h>
#include <stdlib.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


//call back function
int compare(const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

int findLongestConseqSeq(int arr[], const int n)
{
    int length = 1;
    int longestConsecutiveSeq = 1;
    int i =0;


    //sort arr elements using qsort inbuilt function
    qsort( arr,n, sizeof(int), compare);

    for ( i = 0; i < n - 1; i++)
    {
        if(arr[i] == arr[i+1])
        {
            continue;
        }
        else if (arr[i] + 1 == arr[i + 1])
        {
            length++;
        }
        else
        {
            length = 1;
        }

        longestConsecutiveSeq = (longestConsecutiveSeq > length)? longestConsecutiveSeq: length;
    }


    return longestConsecutiveSeq;
}


int main()
{
    int arr[] = {2,5,7,7,8,8,9,4,10,12,3,6};

    const int N = ARRAY_SIZE(arr);

    const int longestConsecutiveSeq = findLongestConseqSeq(arr, N);

    printf("Longest Consecutive Sequence >> %d\n\n",longestConsecutiveSeq);

    return 0;
}

C Program to find length of longest consecutive elements sequence from given unsorted array of integers