59. Scan Memory for Three Consecutive Increasing Values

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int find_pattern(int *mem, int n) {
    // Write your pointer-based logic here
    //Have to crawl along the arr and compare n with n+1 & see if it is +1, if not, set back to n & repeat until 3 times
    int count=0;
    for(int i=0;i<n;i++){
        int next_val = *(mem+i+1); //get next val
        if(next_val == *(mem+i) + 1){ //If next value is an increment of the current value, increase the counter
            count++;
            //printf("Count ++ at index %d\n",i);
        }
        else{
            count = 0; //If not incrementing, reset the counter
        }
        if(count >= 2){
            //printf("Increment found at index %d\n",i);
            //Return index of first element of 3
            return i-1;
        }

    }
    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

 

 

 

Was this helpful?
Upvote
Downvote