All submissions

Scan Memory for Three Consecutive Increasing Values

Code

#include <stdio.h>

int find_pattern(int *mem, int n) {
    int *p = mem;
    for (int i = 0; i <= n - 3; i++) {
        if (*(p + 1) == *p + 1 && *(p + 2) == *(p + 1) + 1)
            return i;
        p++;
    }
    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;
}

Solving Approach

  • Walk through memory with a pointer: Start from the first element and check each group of three consecutive integers using pointer arithmetic.
  • Check the pattern: At each step, verify if *(p+1) == *p + 1 and *(p+2) == *(p+1) + 1. If true, return the current index; else, continue.

 

 

Loading...

Input

8 2 4 5 6 9 11 12 14

Expected Output

1