All submissions

Detect Alternating Pattern

Code

#include <stdio.h>

int is_alternating_pattern(int *start, int k) {

    int expected=!(*start);
    while(k-1)
    {
        //printf("DEBUG: current element:%d and expecting:%d\n",*start,expected);
       if(*(start+1)== expected ) //we will come into this condition until we are keep on satisying the condition.
       {
         start++;
         expected=!(*start);
       }
       else 
         return 0;

        
        k--;
    }


    return 1;
}

int main() {
    int n, k, arr[100];
    scanf("%d %d", &n, &k);

    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int res = is_alternating_pattern(arr, k);
    printf("%d", res);

    return 0;
}

Solving Approach

1: Actually they are very interesting and tough to get caught of an idea to predict the pattern.

2: Initially take a variable: expected and initialize in such a way that expected=!(start), we should have *(start+1)==expected, it means that if we have element in current base address(100) as 1 and  in address 104 we are expecting 0, if this is satisfied we increment the start pointer and update the next expected value.

Else return 0.

 

 

Loading...

Input

6 6 1 0 1 0 1 0

Expected Output

1