# Maximum size of sub-array that satisfies the given condition in C++

In this tutorial, we will be discussing a program to find maximum size of sub-array that satisfies the given condition.

For this we will be provided with an array of integers. Our task is to find the maximum length subset of that array satisfying either of arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even, arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k is odd.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;
//comparing values of a and b
int cmp(int a, int b) {
return (a > b) - (a < b);
}
//returning longest substring
int maxSubarraySize(int arr[], int n) {
int ans = 1;
int anchor = 0;
for (int i = 1; i < n; i++) {
int c = cmp(arr[i - 1], arr[i]);
if (c == 0)
anchor = i;
else if (i == n - 1 || c * cmp(arr[i], arr[i +
1]) != -1) {
ans = max(ans, i - anchor + 1);
anchor = i;
}
}
return ans;
}
int main() {
int arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSubarraySize(arr, n);
}

## Output

5