All submissions

Code

#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

 

  1. XOR all bytes except the last one to compute the expected checksum.
  2. Compare the computed checksum with the last byte in the array.
  3. Return 1 if they match (valid), else return 0 (corrupted).

Simple, fast, and perfect for embedded integrity checks.

 

 

Loading...

Input

5 10 20 30 40 60

Expected Output

0