All submissions

Checksum Validation

Code

#include <stdio.h>

int validate_checksum(int *mem, int n) {

    // init checksum to the first element
    int check_sum = *mem;

    // increase the mem location
    mem++;

    // start from the second element(i=1) to n-1
    for(int i=1;i<n-1;i++)
    {
        check_sum^=*mem;
        mem++;
    }

    if (check_sum == *mem) {
        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

 

 

 

Loading...

Input

5 10 20 30 40 60

Expected Output

0