Skip to main content
1 of 2
wizzwizz4
  • 2.4k
  • 16
  • 42

C (tcc), 56 bytes

f(int*p,int*q){for(int c=0;p-q;*p+=++c)c*=!(*p-*++p-c);}

Try it online!

This is a rather straightforward port of my suboptimal Python answer. Takes a pointer to the beginning and end of the array.

Explanation

f(
    int *p,  /* pointer to the first integer */
    int *q   /* pointer to the last integer */
) {
    for (
        int c=0;  /* the increment */
        p-q;  /* reached the end? (if so, stop) */

        /* note: these run out of order */
                         *p+=++c)
        c*=!(*p-*++p-c);
        /*
            equivalent to:
                int original_p0 = p[0] - c;  // undo increment
                if (original_p0 != p[1]) {
                    c = 0;  // different, so reset counter
                }
                p[1] += c;
                c += 1;
                p += 1; // move to next p
        */
}
wizzwizz4
  • 2.4k
  • 16
  • 42