Code

#include <stdio.h>

int validate_checksum(int *mem, int n) {
    int checksum = 0;
    for ( int i = 0; i < n - 1; i++){
        checksum ^= mem[i];
    }
    if ( checksum == mem(n-1)){
        return 1;
    } else {
        return 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

  1. Start with checksum = 0.
  2. XOR all bytes except the last one → checksum ^= mem[i].
  3. Compare the calculated checksum with the last byte.
  4. If both are equal → return 1 (valid), else return 0 (invalid).

 

 

 

Upvote
Downvote
Loading...

Input

5 10 20 30 40 60

Expected Output

0