Pepper and Contiguous Even Subarray

You have an array of lengths N. A subarray is called Interesting if it contains only even numbers. You have to find the maximum length of such an Interesting subarray.

Example:

Input:  n = 4, a = [5,2,4,7]
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long continuosEvenSubarray(long long nlong long a[])
{
    long long res = 0cnt = 0;
    for (long long i = 0i < ni++)
    {
        if (a[i] & 1)
        {
            if (cnt > res)
                res = cnt;
            cnt = 0;
        }
        else
        {
            cnt++;
        }
    }
    res = max(cntres);
    if (res == 0)
        return -1;
    else
        return res;
}
int main()
{
    long long n = 4;

    long long a[n] = {5247};

    cout << continuosEvenSubarray(na<< "\n";

    return 0;
}


No comments:

Post a Comment