All submissions

Detect Circular Pattern Match

Code

#include <stdio.h>
#include <stdint.h>

uint16_t circular_buffer(uint16_t reg1, uint16_t reg2){
    for (int i=0; i < 15; i++){
        reg1 <<= 1;

    }
    // printBinary(reg1|reg2);
    return reg1|reg2; 
}

uint8_t is_circular_match(uint16_t reg, uint16_t target)
{
    // Your code here
    uint16_t mask = 0;
    int n = 16;
    uint16_t result = reg ^ target;

    if (result > 0)
    {
        while (n>=0)
        {
            mask |= (reg & 1U);
            reg >>= 1;
            n--;

            reg = circular_buffer(mask, reg); 
            result = reg^target; 
            if (result ==0){
                return 1;
            }
            mask = 0;
            
        }
    }else 
    {
        return 1;
    }

    return 0;
}

int main()
{
    uint16_t reg, target;
    scanf("%hu %hu", &reg, &target);
    printf("%hhu", is_circular_match(reg, target));
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

45056 11

Expected Output

1