Loop that covers from 0 to i - 1 and i + 1 to n - 1 indexes simultaneously
for ( $ j = $ i - 1 , $ k = $ i + 1 ; $ j > 0 $ k <= $ n ; -- $ j , ++ $ k ) { if ( $ j >= 0 && __gcd ( $ arr [ $ i ] , $ arr [ $ j ] ) > 1 ) { $ closest = $ j + 1 ; break ; } if ( $ k < $ n && __gcd ( $ arr [ $ i ] , $ arr [ $ k ] ) > 1 ) { $ closest = $ k + 1 ; break ; } }