#include <stdio.h>
int validate_checksum(int *mem, int n) {
// Write your XOR scan logic here
if(n < 2){
return 0; // not enough data
}
int xor_sum = 0;
for(int i = 0; i < n - 1; ++i){
xor_sum ^= mem[i];
}
return (xor_sum == 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;
}
Solving Approach
Questions:
1. Input constraints
- n >= 2
- values in mem [0,255]
2. Edge Cases
- what should the function return if n <2
- should the function handle negative values or values > 255
3. Function Signature
- is the function signature fixed?
- should it print along with returning the value?
Plan:
1. Input validation
2. XOR calculation
3. Checksum comparision
4. Main function
Edge cases:
- n < 2
- all bytes are 0
- large values (input > 255)
- only one data byte
Input
5 10 20 30 40 60
Expected Output
0