All submissions

Detect Alternating Pattern

Code

#include <stdio.h>

int is_alternating_pattern(int *mem, int k) {
    // Write your pointer logic here
    int odd_value,even_value;
    even_value = mem[0];
    odd_value = mem[1];
    for(int i = 2; i < k;i++)
    {
        if(i%2 == 0)
        {
            if(mem[i]!=even_value)
            {
                return 0;
            }
        }
        else
        {
            if(mem[i]!=odd_value)
            {
                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

 

 

 

Loading...

Input

6 6 1 0 1 0 1 0

Expected Output

1