The longest sequence of the same value, C, array

I need to find the longest sequence of the same value in the array and return the length of this sequence. For example, for the int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};answer should be 3, because we have three fours. However, my code always returns 0, and I don't know why:

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 0, len = 0;
    int i;

    for(i=0; i<n; i++)
    {
        if(tab[i+1] == tab[i])
        {
            len ++;
            if (len > previous_len)
            {
                previous_len = len;
            }
        }
        else
        {
            previous_len = len;
            len = 0;
        }
    }
    return len;
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};
    int n = 9;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}

EDIT

Some errors have been changed, as indicated in the comments, however the result is now 1:

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 1, len = 1;
    int i;

    for(i=0; i<n-1; i++)
    {
        if(tab[i+1] == tab[i])
        {
            len ++;
            printf("len = %d\n", len);

            if (len > previous_len)
            {
                previous_len = len;
            }
        }
        else
        {
            previous_len = len;
            len = 1;
        }
    }
    return len;
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 1, 3};
    int n = 9;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}

EDIT

It seems ok now :

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

int longest_sequence(int *tab, int n)
{
    int previous_len = 1, len = 1, max = 0;
    int i;

    if(n == 0)
    {
        return 0;
    }
    else
    {
        for(i=0; i<n-1; i++)
        {
            if(tab[i+1] == tab[i])
            {
                len ++;

                if (len > previous_len)
                {
                    previous_len = len;
                }
            }
            else
            {
                previous_len = len;
                len = 1;
            }

            if(len > max)
                max = len;
        }
        return max;
    }
}

int main()
{
    int tab[] = {2, 2, 4, 4, 4, 2, 2, 2, 2, 1, 3};
    int n = 11;

    int res = longest_sequence(tab, n);
    printf("%d\n", res);
    return 0;
}
+4
source share
3 answers

Here you

#include <stdio.h>

size_t longest_sequence( const int *a, size_t n )
{
    size_t len = 0;

    for ( const int *p = a; p != a + n; )
    {
        const int *q = p++;
        while ( p != a + n && *p == *q ) ++p;
        if ( len < ( size_t )( p - q ) ) len = p - q;
    }        

    return len;
}    

int main( void ) 
{
    int tab[] = { 2, 2, 4, 4, 4, 2, 2, 1, 3 };
    const size_t N = sizeof( tab ) / sizeof( *tab );

    size_t len = longest_sequence( tab, N );

    printf( "%zu\n", len );

    return 0;
}

Program exit

3

Please note that, as usual, my solution is the best among those presented here. :)

+3
source

, , , , , , , reset , reset

int
longest_sequence(int *array, int count)
{
    int length;
    int longest;
    longest = length = 0;
    for (int i = 0 ; i < count - 1 ; i++)
    {
        if (array[i + 1] == array[i])
            length++;
        else
        {
             // length + 1 to count the last element too
            if (longest < length + 1)
                longest = length + 1;
            length = 0;
        }
    }
    return longest;
}

int
main()
{
    int array[] = {2, 2, 4, 4, 4, 5, 2, 2, 1, 3};
    int count = sizeof(array) / sizeof(array[0]);

    printf("The longest sequence has `%d' elements.\n", 
        longest_sequence(array, count));
    return 0;
}
+2

There are problems in your code:

 else
 {
    if (len > previous_len) // <<<< you are missing this test
      previous_len = len;

    len = 1;
 }
    return previous_len;    // <<<< return previous_len instead of len
0
source

Source: https://habr.com/ru/post/1629373/


All Articles