4 ms·
(a / 2) + (b / 2) + ((a % 2) + ((b % 2)) / 2) works and can be extended from 2 to n to find the average of a set of integers.
by shkkmo 4y ago
(a / 2) + (b / 2) + ((a % 2) + ((b % 2)) / 2) works and can be extended from 2 to n to find the average of a set of integers.
- a1369209993 4y agoNote that this can equivalently[0] (and more readably) be written as `(a/2) + (b/2) + (a&b&1)`. 0: Assuming your language rounds integer division and modulo correctly, ie that `i%array_len` is reliably a valid array index. C has problems here when i (respectively: a or b) is signed, but that doesn't matter in the sensible case, where you always index everything with size_t.