#include <stdio.h>
#include <math.h>
int find_pattern(int *mem, int n) {
int l = 0; // Left pointer of window
int r = 1; // Right pointer of window
while (r < n) {
if (*(mem + r) - *(mem + (r-1)) != 1)
l = r;
if ((r-l) == 2)
return l;
r++;
}
return -1;
}
int main() {
int n, arr[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int res = find_pattern(arr, n);
printf("%d", res);
return 0;
}
l
→ marks the start of the current window (potential starting index).r
→ moves forward to scan through the array.*(mem + r)
with the previous element *(mem + (r - 1))
.1
, it means the sequence is still increasing consecutively.l = r
.(r - l)
.2
, it means there are three consecutive increasing integers:*(mem + l)
, *(mem + l + 1)
, and *(mem + r)
.l
.-1
.
Input
8 2 4 5 6 9 11 12 14
Expected Output
1