Detect Circular Pattern Match

Code

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

// reg:    1010 1010 1010 1010
// reg<<8: 1010 1010 ____ ____
//reg>>8:  ____ ____ 1010 1010

uint8_t is_circular_match(uint16_t reg, uint16_t target) {
    // Your code here
    for(int i=0;i<16;i++) {
        uint16_t rotated = ((reg<<i)|(reg>>(15-(i-1))));
        if( target==rotated)
            return 1;
    }
    return 0;
}

int main() {
    uint16_t reg, target;
    scanf("%hu %hu", &reg, &target);
    printf("%hhu", is_circular_match(reg, target));
    return 0;
}
// shifting by 0 = shifting by n
// 100
// left shifted by 0-> 100
// left shifted by 3 ->100
// so check from 1 -> n-1

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

45056 11

Expected Output

1