You are given a block of memory (array of n bytes) which includes n-1 data bytes and the last byte as checksum.
Your task is to verify whether the last byte equals the XOR of all previous bytes (excluding itself).
Return:
1 if the checksum is valid
0 if the checksum is incorrect
Checksum: It’s a simple error-detection method that ensures data hasn’t been corrupted during storage or transmission.
The sender calculates based on specific algorithm and sends it with the data; the receiver recalculates it similarly from the received data—if both match, the data is intact, otherwise it’s likely corrupted.
Some methods used are
Simple Sum Add all bytes, keep only the lowest 8 or 16 bits.
XOR Checksum: XOR all bytes of data.
CRC (Cyclic Redundancy Check): more accurate, etc
Example-1
Input: n = 5, mem = [10, 20, 30, 40, 60]
Output: 1