Detect Circular Pattern Match

Code

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

int is_circular_match(uint16_t reg, uint16_t target){
    uint16_t byte = 0;
    for(int i = 0; i < 16; i++){
        // Check left rotation
            uint16_t check_left_rotation = (reg << i) | (reg >> (16 - i));
            if(check_left_rotation == target) return 1;
        // Check right rotation
            uint16_t check_right_rotation = (reg >> i) | (reg << (16 - i));
            if(check_right_rotation == target) return 1;
    }
    return 0;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

45056 11

Expected Output

1