All submissions

Scan Memory for Three Consecutive Increasing Values

Code

#include <stdio.h>
#include <stdbool.h>

int find_pattern(int *mem, int n) {
    
    bool isPatternDetected = false;

    int a=0,b=0,c=0;
    int i=0;

    for(i=0;i<n;i++)
    {
        a = *mem;
        b = *(mem+1);
        c = *(mem+2);

        if((a+1 == b) && (b+1 ==c))
        {
            isPatternDetected = true;
            break;
        }  

        if(i>n-2)
        {
            break;
        }

        mem++;  
    }

    if(isPatternDetected)
    {
        return i;
    }

    else
    {
        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

 

 

 

Loading...

Input

8 2 4 5 6 9 11 12 14

Expected Output

1