n; // define sum as an integer f(x,y,a,b) // function taking two arrays and two lengths int*x,*y; // use k&r style definitions to shorten function declaration { for(n=0; // initialize sum to 0 a;) // keep looping until x (the first array) runs out // we'll decrement a/b every time we increment x/y respectively --b&& // if y has ≥1 elements left (b>1, but decrements in-place)... *x*2-*y>y[1]? // ... and x - y > [next y] - x, but rearranged for brevity... ++y: // increment y (we already decremented b earlier); (++b, // otherwise, undo the in-place decrement of b from before... --a,n+=abs(*x++-*y)) // decrement a instead, add |x-y| to n, and then increment x ;} Some notes:
Instead of indexing into the arrays, incrementing the pointers and dereferencing directly saves enough bytes for it to be worth it (
*xvsx[a]andy[1]vsy[b+1]).The
--b&&condition checks forb>1in a roundabout way - ifbis1, it will evaluate to zero. Since this modifiesb, we don't need to change it in the first branch of the ternary (which advancesy), but we do need to change it back in the second (which advancesx).No
returnstatement is needed, because black magic. (I think it's because the last statement to be evaluated will always be then+=...expression, which uses the same register as the one used for return values.)