Here I am using 2 arrays to calculate GCD from the starting and from the end. A[i] is the array given for which I am calculating GCD. If I am using the commented part of code to calculate and store GCD's then output I am getting is correct but I am getting TLE error for higher inputs while the statements " backward[i] = gcd(backward[i+1], A[i]); " and " forward[i] = gcd(forward[i-1], A[i]); " are showing correct results without TLE.
Can someone explain what is the difference between the above 2 kinds of statements. why the commented statements are giving TLE ?
'''
int gcd_f = 0, gcd_b = 0;
for(int i = 1; i <= N; i++){
**//gcd_f = gcd(gcd_f, A[i]);
//forward[i] = gcd_f;**
forward[i] = __gcd(forward[i-1], A[i]);
}
for(int i = N; i >= 1; i--){
**//gcd_b = gcd(gcd_b, A[i]);
//backward[i] = gcd_b;**
backward[i] = gcd(backward[i+1], A[i]);
}
'''