All submissions

Checksum Validation

 

#include <stdio.h>

int validate_checksum(int *mem, int n) {
    // Write your XOR scan logic here
     int calculated_xor = 0;
    
    //  XOR of all previous bytes (excluding itself)
    for (int i = 0; i < n - 1; i++) 
        calculated_xor ^= mem[i];
    
    // Compare calculated XOR with the provided checksum (last byte)
    return (calculated_xor == mem[n - 1]) ? 1 : 0;
}

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;
}

 

 

 

 

Loading...

Input

5 10 20 30 40 60

Expected Output

0