Question: C Programming Language imgur mirror: https://i.imgur.com/fZLTRV1.png The code below runs well, but it runs into a time limit error. Can anyone modify the code so
C Programming Language
imgur mirror: https://i.imgur.com/fZLTRV1.png
The code below runs well, but it runs into a time limit error. Can anyone modify the code so that it uses fast IO or is otherwise more efficient?
#include
int gcd(int n1, int n2) { int i, gcd; for(i=1; i
int findGGCD(int arr[], int n) { int i, j; int ggcd = 0; for (i = 0; i arr[j+1] && arr[j+1]!=0) { k = gcd(arr[j+1], arr[i] % arr[j+1]); } else if(arr[i] > arr[j+1] && arr[j+1]==0) { k = arr[i]; } else { k = gcd(arr[i], arr[j+1]); } if(k > ggcd) { ggcd = k; } } } return ggcd; }
int main() { int cases; scanf("%d", &cases); if(cases100) { return 1; } for(int q = 1; q100) { return 1; } int ggcd; int arr[n]; for(int i = 0; i { scanf(" %d", &arr[i]); if(arr[i]1000000000) { return 1; } } ggcd = findGGCD(arr, n); printf("Case #%d: %lld ", q, ggcd); } }
Euclidean algorithm is an efficient method for computing greatest common divisor (GCD) of 2 numbers, the largest positive integer that divides both of them without leaving a remainder. For example, the GCD of 8 and 12 is 4. To calculate the GCD, we could use the equation below GCD(a,b)-GCD(b,a%b) GCD(a,b)- a if a b and b-0 if a> b and b 0 This problem is very simple, you just have to read N number and print the greatest of GCD (GGCD). To find GGCD, you must find the GCD of all pairs (a, a) where i!j and find the largest GCD Format Input The input starts with an integer T represents the number of test cases. Each test case will start with an integer N, the number of numbers to read. The next line will contain N integers a, as the numbers to be read. Format Output For each test case, print "Case #X: Y" where X is the test number and Y is the GGCD Constraints 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
