You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive
MY Approach :
public static int gcd(int a ,int b) {
if(b == 0) return a;
return gcd(b, a % b);
}
for(int j = 0; j < Q; j++) {
int l = in.nextInt();
int r = in.nextInt();
ans = 0;
for(int k = 1; k <= n; k++) {
if(k < l || k > r) ans = gcd(a[k], ans);
}
System.out.println(ans);
}
But This approach give me Time Limit Exceeded Error How can i improve my alogrithm
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…