Let us look at what one operation really means.
Suppose we are working with a subarray $$$[l,r]$$$, and let $$$x=a_l$$$. The first element is special because it never changes: every operation only uses earlier values to modify later positions, so the leftmost value stays fixed forever.
Now the only thing that matters is how much freedom we have for the next elements. This is where $$$\text{gcd}$$$ enters naturally. If the current prefix has $$$\text{gcd}$$$ equal to $$$g$$$, then we claim that the next element can be changed by any multiple of $$$g$$$.
ProofLet the array be $$$b$$$, and we are focusing on a prefix $$$b_1, b_2, \dots, b_{k-1}$$$ to modify the next element $$$b_k$$$.
The allowed operation is strictly adjacent: $$$b_{j+1} := b_{j+1} \pm b_j$$$. At first glance, it seems we can only directly add $$$b_{k-1}$$$ to $$$b_k$$$. How do we add an earlier element, like $$$b_1$$$, directly to $$$b_k$$$ without permanently destroying the intermediate elements?
We can "pass" values through intermediate elements. Let's look at adding $$$b_1$$$ to $$$b_3$$$ without permanently changing $$$b_2$$$:
- Add $$$b_1$$$ to $$$b_2 \implies b_2$$$ becomes $$$b_2 + b_1$$$
- Add $$$b_2$$$ to $$$b_3 \implies b_3$$$ becomes $$$b_3 + b_2 + b_1$$$
- Subtract $$$b_1$$$ from $$$b_2 \implies b_2$$$ is restored to its original value $$$b_2$$$
- Subtract $$$b_2$$$ from $$$b_3 \implies b_3$$$ becomes $$$(b_3 + b_2 + b_1) - b_2 = b_3 + b_1$$$
Through this sequence, $$$b_2$$$ is perfectly restored to its original state, but $$$b_3$$$ has cleanly absorbed exactly one $$$+b_1$$$.
By chaining this trick across any distance, we can propagate any prefix element $$$b_i$$$ to $$$b_k$$$. We can repeat these sequences to add or subtract $$$b_i$$$ as many times as we want. Because these operations can be done independently for each element in the prefix, the total change we can apply to $$$b_k$$$ is precisely the set of all integer linear combinations:
$$$\Delta b_k = c_1b_1 + c_2b_2 + \dots + c_{k-1}b_{k-1}$$$where $$$c_i \in \mathbb{Z}$$$.
Let $$$C$$$ be the set of all integer linear combinations of the prefix elements $$$b_1, \dots, b_{k-1}$$$, and let $$$g = \gcd(b_1, \dots, b_{k-1})$$$. Let $$$M$$$ be the set of all integer multiples of $$$g$$$. We need to prove that $$$C = M$$$ by showing they are subsets of each other.
1. Every linear combination is a multiple of $$$g$$$ ($$$C \subseteq M$$$)
By the definition of the greatest common divisor, $$$g$$$ divides every element $$$b_i$$$. Thus, there exist integers $$$q_i$$$ such that $$$b_i = q_i g$$$. Take any arbitrary linear combination $$$x \in C$$$:
$$$x = \sum_{i=1}^{k-1} c_i b_i$$$Substitute $$$b_i = q_i g$$$ into the equation:
$$$x = \sum_{i=1}^{k-1} c_i (q_i g) = g \sum_{i=1}^{k-1} (c_i q_i)$$$Since the product and sum of integers $$$c_i$$$ and $$$q_i$$$ form an integer, $$$x$$$ is an exact integer multiple of $$$g$$$. Therefore, $$$C \subseteq M$$$.
2. Every multiple of $$$g$$$ is a reachable linear combination ($$$M \subseteq C$$$)
By Bézout's Identity, there exist integers $$$u_i$$$ such that their linear combination exactly equals the greatest common divisor:
$$$\sum_{i=1}^{k-1} u_i b_i = g$$$Take any arbitrary multiple $$$y \in M$$$, meaning $$$y = m g$$$ for some integer $$$m$$$. Substitute $$$g$$$ using Bézout's Identity:
$$$y = m \left( \sum_{i=1}^{k-1} u_i b_i \right) = \sum_{i=1}^{k-1} (m u_i) b_i$$$Since $$$m$$$ and $$$u_i$$$ are integers, their product $$$(m u_i)$$$ is also an integer. This proves $$$y$$$ is a valid linear combination of the prefix elements. Therefore, $$$M \subseteq C$$$.
Since $$$C \subseteq M$$$ and $$$M \subseteq C$$$, it strictly follows that $$$C = M$$$. The set of all reachable changes is exactly the set of all multiples of the prefix's $$$\gcd$$$.
Now let us focus on subarrays starting at index $$$l$$$.
As long as every element to the right of $$$a_l$$$ is divisible by $$$a_l$$$, the $$$\text{gcd}$$$ of the processed prefix remains exactly $$$a_l$$$ — because all seen values are multiples of $$$a_l$$$, and $$$a_l$$$ itself is present. So every such element can be moved by multiples of $$$a_l$$$, which means each of them can be made equal to $$$a_l$$$. Therefore, if the subarray never contains an element not divisible by $$$a_l$$$, then the whole subarray can be made constant, and the value of $$$f$$$ is $$$0$$$.
Now define $$$\text{next[l]}$$$ as the first position $$$t \gt l$$$ such that $$$a_t$$$ is not divisible by $$$a_l$$$. If no such position exists, set $$$\text{next[l]=n+1}$$$.
If such a position exists, we write
$$$ r = a_t \bmod a_l. $$$Since $$$t$$$ is the first such position, the $$$\text{gcd}$$$ of everything before it is still $$$a_l$$$, so the value at position $$$t$$$ can only move inside its residue class modulo $$$a_l$$$. Hence, the closest it can get to $$$a_l$$$ is
$$$ d=\min(r,\ a_l-r). $$$So the spread of the subarray can never go below $$$d$$$.
The key observation is that the first non-divisible element already fixes the answer. The lower bound $$$d$$$ obtained from this element is tight, and no later element can increase it. Therefore, once this position appears, every longer subarray starting at $$$l$$$ has the same answer $$$d$$$.
ProofLook at the position $$$t + 1$$$. The prefix $$$\text{gcd}$$$ at this position will be $$$g = \text{gcd}(a_l, a_t)$$$. Since $$$g$$$ divides both $$$a_l$$$ and $$$a_t$$$, it also divides $$$a_t \bmod a_l$$$ and $$$a_l - a_t \bmod a_l$$$. Therefore, $$$g \mid d$$$.
From this point onward, every later element can be modified using multiples of a $$$\text{gcd}$$$, which is at most $$$g$$$. Therefore, every later element can be adjusted in steps whose size divides $$$d$$$.
Now, fix the value at position $$$t$$$ so that its distance from $$$a_l$$$ is exactly $$$d$$$. Since every future modification is performed using multiples of divisors of $$$d$$$, every later element can be moved into the interval of length $$$d$$$ determined by these two values. Consequently, no later position can force the spread to become larger than $$$d$$$.
Therefore, the first non-divisible element completely determines the answer: once position $$$t$$$ appears, every longer subarray contributes the same value $$$d$$$ to the final answer.
Hence, for a fixed $$$l$$$:
- for every $$$r \lt \text{next}[l]$$$, we have $$$f([a_l,\dots,a_r])=0$$$;
- for every $$$r\ge \text{next}[l]$$$, we have
$$$ f([a_l,\dots,a_r]) = \min \bigl(a_{\text{next}[l]} \bmod a_l,\ a_l-(a_{\text{next}[l]} \bmod a_l)\bigr). $$$So, once $$$\text{next[l]}$$$ is found, all longer subarrays starting at $$$l$$$ contribute the same value. That makes counting easy. The number of subarrays starting at $$$l$$$ that contain this position is $$$n-t+1$$$. So the contribution from index $$$l$$$ is
$$$ (n-t+1) \cdot \min \bigl(a_t\bmod a_l,\ a_l-(a_t\bmod a_l)\bigr). $$$Now we only need to find $$$\text{next[l]}$$$ for every $$$l$$$. This can be done with a monotonic stack while scanning from left to right. The stack stores indices whose first non-divisible element has not been found yet. In detail, when we are at position $$$i$$$, if
$$$ a_i \bmod a_{\text{top}} \ne 0, $$$then we have $$$i = \text{next[top]}$$$. Why? Because if any earlier position had already broken divisibility for that index, it would already have been removed from the stack. So we can finalize its contribution immediately and pop it. This way, each index is pushed once and popped once, so the whole procedure is linear.
Time Complexity: $$$\mathcal{O}(n)$$$.
Note that one can also find $$$\text{next[l]}$$$ for every index $$$l$$$ by performing binary search using a sparse table to store $$$\text{gcd}$$$, leading to an $$$\mathcal{O}(n \cdot \log n \cdot \log a)$$$ solution. These implementations are also intended to pass.