Home »
Data Structure
Length of longest increasing circular subarray
Here is the implementation to find length of longest increasing circular subarray
C program to find length of longest increasing circular subarray
#include <stdio.h>
int longestIncreasingCircularSubarray(int arr[], int n) {
if (n == 0) return 0;
// Double the array size for circular nature
int temp[2 * n];
for (int i = 0; i < n; i++) {
temp[i] = arr[i];
temp[n + i] = arr[i];
}
int maxLength = 1;
int currentLength = 1;
// Traverse the doubled array to find the
// maximum length of increasing subarray
for (int i = 1; i < 2 * n; i++) {
if (temp[i] > temp[i - 1]) {
currentLength++;
} else {
currentLength = 1;
}
if (currentLength > maxLength) {
maxLength = currentLength;
}
// Stop if the currentLength reaches the original array size
if (currentLength == n) {
break;
}
}
return maxLength;
}
int main() {
int arr[] = {3, 1, 2, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int result = longestIncreasingCircularSubarray(arr, n);
printf("The length of the longest increasing circular subarray is: %d\n",
result);
return 0;
}
Output
The length of the longest increasing circular subarray is: 4