#include <stdio.h>
int validate_checksum(int *mem, int n) {
int x = 0;
for (int i = 0; i < n - 1; i++) {
x ^= mem[i]; // XOR all data bytes
}
return x == mem[n - 1] ? 1 : 0; // Compare with checksum
}
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
XOR all bytes except the last one to compute the expected checksum.
Compare the computed checksum with the last byte in the array.
Return 1 if they match (valid), else return 0 (corrupted).
Simple, fast, and perfect for embedded integrity checks.