All submissions

Detect Alternating Pattern

Iterate through all elements while checking against next.

#include <stdio.h>

int is_alternating_pattern(int *mem, int k) {
    // Write your pointer logic here
    
    bool curr;      //works because seq is 0 and 1
    while(--k){
        curr = (bool)*mem;
        if(curr == *(mem+1))   return 0;    //can replace with  1-curr

        mem++;
    }
    /*
    while(--k){
        if(*mem == *(++mem))    return 0;
    }
    */
    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

check if the next element is alternate, use *(mem+1).

 

 

Loading...

Input

6 6 1 0 1 0 1 0

Expected Output

1