userA ng
userA ng

Reputation: 53

maximum consecutive repetition in an array

I want to count the maximal consecutive trues in the array. In the follows, it return me 4. It seems that all the trues in the array are added up. What's wrong with my work? Here is my code.

int main()
{
int maxCount = 0;
bool list[7];
list[0] = true;
list[1] = true;
list[2] = false;
list[3] = false;
list[4] = true;
list[5] = true;
list[6] = false;

for (int i = 0; i < 6; i++)
{
    int count = 0;
    if (list[i]==true)
    {
        count++;
        for (int j = i + 1; j < 7; j++)
        {
            if (list[j]== list[i])
            {
                count++;
            }
        }
    }
    if (count > maxCount)
    {
        maxCount = count;
    }
}
cout << maxCount << endl;
}

Upvotes: 0

Views: 160

Answers (4)

songyuanyao
songyuanyao

Reputation: 172924

It's better to consider the relationship between count and maxCount again.

int maxCount = 0;
int count = 0;
int size = sizeof(list) / sizeof(bool);
for (int i = 0; i < size; i++) {
    if (list[i]==true) {
        count++;
    } else {
        if (count > maxCount) {
            maxCount = count;
        }
        count = 0;
    }
}
if (count > maxCount) {
    maxCount = count;
}
cout << maxCount << endl;

LIVE

Upvotes: 0

mkierc
mkierc

Reputation: 1188

You should traverse the array just once, but using two counters:

  • First for current streak of true values
  • Second for highest streak

Every time you encounter a false reset the current streak counter to zero, and every time you encounter a true, increment the value of current streak and check whether it's higher than highest streak counter, and if so, update it.

Upvotes: 0

CinCout
CinCout

Reputation: 9619

The way you are implementing is completely wrong. You are adding all the true entries in the array, without accounting for a false in between.

Do this:

int currentCount = 0, maxCount = 0;
for (int i = 0; i < 7; ++i)
{
    // arr is the name of the array
    if(arr[i])    ++currentCount;
    else    currentCount = 0;  // resetting to zero if false encountered
    maxCount = ( (currentCount > maxCount) ? currentCount : maxCount );
}
cout << maxCount << endl;

Upvotes: 1

Tio Pepe
Tio Pepe

Reputation: 3089

Break on true/false changes and be careful of only last true

#include <iostream>

int main()
{
    using namespace std;

    int maxCount = 0;
    bool list[7];
    list[0] = true;
    list[1] = true;
    list[2] = false;
    list[3] = false;
    list[4] = true;
    list[5] = true;
    list[6] = false;

    for (int i = 0; i < 7; i++) // be careful last true
    {
       int count = 0;
       if (list[i]==true)
       {
           count++;
           for (int j = i + 1; j < 7; j++)
           {
               if (list[j]== list[i])
               {
                   count++;
               }
               else
               {
                   break; // true / false transition
               }
           }
        }
        if (count > maxCount)
        {
            maxCount = count;
        }
    }
    cout << maxCount << endl;
}

Upvotes: 0

Related Questions