All submissions

Scan Memory for Three Consecutive Increasing Values

Code

#include <stdio.h>

int find_pattern(int *mem, int n) {
    // Write your pointer-based logic here
    int count = 0,index = 0,i=0;
     for(i=0;i<n;i++){                  // run loop n times
       if(*(mem+i) == *(mem+i+1)-1){    // checking the present value and next value -1 is equal to or not 
             count++;                   // if same increment count
         
         if(count == 1)                 // if count is 1 store the i value in index
          index = i;

         if(count == 2){                // we found here 3 consecutive numbers 
             return index;              // return index
         }
       } else                           // else make count zero
        count = 0;
    }
    return -1;                          // if nothing found 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

 

int find_pattern(int *mem, int n) {

    // Write your pointer-based logic here

    int count = 0,index = 0,i=0;

     for(i=0;i<n;i++){                  // run loop n times

       if(*(mem+i) == *(mem+i+1)-1){    // checking the present value and next value -1 is equal to or not 

             count++;                   // if same increment count

         

         if(count == 1)                 // if count is 1 store the i value in index

          index = i;


 

         if(count == 2){                // we found here 3 consecutive numbers 

             return index;              // return index

         }

       } else                           // else make count zero

        count = 0;

    }

    return -1;                          // if nothing found return -1

}


 

 

Loading...

Input

8 2 4 5 6 9 11 12 14

Expected Output

1