Checksum Validation

Code

#include <stdio.h>
#include <stdint.h>
int validate_checksum(int *mem, int n) 
{
    // Write your XOR scan logic here
    uint8_t res = 0x00; 
    for (int i = 0; i < n-1; i++)
    {
        res = res ^ mem[i];
    }
    //printf("%d\n",res);
    int byte8 = mem[n-1];
    //printf("%d\n",byte8);
    if (res != byte8)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main() {
    int n, arr[100];
    scanf("%d", &n);
    
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int result = validate_checksum(arr, n);
    printf("%d", result);

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5 10 20 30 40 60

Expected Output

0