For both codes l is initially 1 , all variables except the array are long long , array is global int of size N. N is to the order of 10^6. But code 1 exceeds time limit of 5 seconds but code 2 runs in milliseconds. The only difference is in the last line. a,b,c are integer variables less than 50.
Compiler used :- g++
test case :- 3000000 50 50 50 1 (N,a,b,c,l)
Code 1:-
for(i=1;i<N;i++) { j=l; k=(j*j)%1000000; k=(a*k)%1000000; j=(b*j)%1000000; l=(j+k+c)%1000000; x=(int)l; ar[i]=x; } Code 2:-
for(i=1;i<N;i++) { j=l; k=(j*j)%1000000; k=(a*k)%1000000; j=(b*j)%1000000; l=(j+k+c)%1000000; x=(int)l; ar[i]=1; }