# c++ – Calculate the maximum number of negative elements in a row

## Question:

It is necessary to calculate the maximum number of negative elements in a row. I can't figure out how to do it right. At the moment, it just counts all negative elements.

``````#include <iostream>
#include<time.h>
using namespace std;

void main()
{
setlocale(LC_ALL, "ukr");
srand(time(NULL));
const int n = 15;
bool change = true;
int arr[n], m = 0;

for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10 - 5;
cout << arr[i] << " ";
}
cout << endl;
int maxcount = 0;
for (int i = 0; i < n; i++)
{
int count = 0;
if (arr[i] < 0) {
for (int j = i; j < n; j++)
{
if (arr[j] < 0) {
count++;
if (count > maxcount) { maxcount = count; }
cout << count << " ";
}
}
}
}
cout << endl << maxcount << endl;
}
``````

A little bit of functionality for an amateur:

``````#include <range/v3/all.hpp>

int main()
{
int v[] { -2, 0, 2, -1, -2, -11, -2, 3, 5, 1, -1, -1, 1, -2, -1, 2 };

int m = ranges::max(v
| ranges::view::group_by([](int a, int b){return (a < 0) && (b < 0);})
| ranges::view::transform(ranges::distance)
);

assert(m == 4);
}
``````

Now, if more ranges in the standard library zaapruvili .. Yes, and with parallel algorithms .. hmmm …

Scroll to Top