As we know, if $$$x!=1$$$ and $$$x!=y$$$, then $$$gcd(x,y)<=x/2$$$. That means for each element $$$a_i$$$ in the array, it will change at most $$$log(a_i)$$$ times before it becomes $$$1$$$.
So we can implement the process, but for every round we only consider the element that will change (which means $$$a_i != 1$$$ and $$$a_i != a_{i+1}$$$, then we just do $$$O(N*log(a_i))$$$ times change.
How to find the elements that will change in the next round? We can see that elements will change in the next round, are only in the elements change in this round and their left element, so we just check them. The number of elements we check is $$$O(N*log(a_i))$$$ too.
solution