0

I have a 1-dimensional array that fills up a table of 40 random elements (all the values are either 0 or 1). I want to find the longest consecutive span of values.

For example:

In 111100101 the longest row would be 1111 because it has four consecutive values of 1.

In 011100 the result is 111.

I have no idea how to check upon the "next element" and check if it's a 0 or 1. Like the first would be 1111 (count 4) but the next would be a 0 value, meaning I have to stop counting.

My idea was placing this value (4) in a other array (example: 111100101), and place the value of the 1's back on zero. And start the process all over again.

To find the largest value I have made another method that checks up the biggest value in the array that keeps track of the count of 0's 1's, this is not the problem.

But I cannot find a way to fill the array tabelLdr up, having all the values of the group of elements of the same kind (being 0 or 1).

In the code below I have 2 if's and of course it will never go into the second if (to check if the next value in the array is != to its current state (being 0 or 1).

public void BerekenDeelrij(byte[] tabel, byte[] tabelLdr) 
{
    byte LdrNul = 0, Ldréén = 0;
    //byte teller = 0;

    for (byte i = 0; i < tabel.Length; i++) 
    {
        if (tabel[i] == 0) 
        {
            LdrNul++;
            //this 2nd if cleary does not work, but i have no idea how to implend this sort of idea in my program.
            if (tabel[i] == 1) //if value != 0 then the total value gets put in the second array tabelLdr, 
            {
                tabelLdr[i] = LdrNul;
                LdrNul = 0
            }
        }

        if (tabel[i] == 1)
        {
            Ldréén++;
            if (tabel[i] == 0)
            {
                tabelLdr[i] = Ldréén;
                Ldréén = 0;
            }
        }

    }/*for*/
}

3 Answers 3

1

This method should do what you need:

public int LargestSequence(byte[] array) {
  byte? last = null;
  int count = 0;
  int largest = 0;
  foreach (byte b in array) {
    if (last == b)
      ++count;
    else {
      largest = Math.Max(largest, count);
      last = b;
      count = 1;
    }
  }
  return Math.Max(largest, count);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Even while i is the loop counter, it is still just a variable. A valid for statement is for (;;), which is an infinite loop. Notice the for statement increments i, as in i++. The expression i = i + 1 works just as well.

Comments

0

Im unsure if you need the longest "row" of ones or longest row of either 0 or 1. This will work for the latter

var max = 0;
var start = 0;
var current = -1;
var count = 0;
for(int i = 0;i<table.Length;i++)
{
   if(current = table[i])
   {
      count++;
   }
   else
   {
      current = table[i];
      if(max < count)
      {
        max = count;
        start = i-count;
      }
      count = 1;
   }
}

 if(max < count)
      {
        max = count;
        start = i-count;
      }

//max is the length of the row starting at start;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.