fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int gcd(int a, int b){
  5. if(b == 0){
  6. return a;
  7. }
  8. return gcd(b, a % b);
  9. }
  10.  
  11. int main(){
  12.  
  13. int r,c;
  14. printf("Enter Row (R) and Column (C) : "); //Taking Row and Column input
  15. scanf("%d%d", &r, &c);
  16.  
  17. int arr[r][c];
  18.  
  19. printf("Enter all the numbers\n"); // Taking all the numbers
  20. for(int i = 0; i<r; i++){
  21. for(int j = 0; j<c; j++){
  22. scanf("%d", &arr[i][j]);
  23. }
  24. }
  25.  
  26. int maximum_gcd[r]; // taking this array to store pairwise gcd
  27. int temp[(c*(c+1))/2]; // taking this array to store all the possible gcd of each row
  28. memset(temp, 0, sizeof(temp)); // clearing garbage value of temp array.
  29.  
  30. int itr = 0; //took this integer to keep track of the indexing in temp array.
  31.  
  32. for(int i = 0; i<r; i++){
  33. itr = 0;
  34. for(int j = 0; j<c-1; j++){
  35. for(int k = j+1; k<c; k++){
  36. temp[itr++] = gcd(arr[i][j], arr[i][k]); //finding all the gcd's
  37. }
  38. }
  39. int maxi = -1;
  40. for(int q= 0; q<(c*(c+1))/2; q++){ // fidning the maximum gcd in temp array
  41. if(maxi<temp[q]){
  42. maxi = temp[q];
  43. }
  44. }
  45. maximum_gcd[i] = maxi; // keeping the maximum gcd in the array.
  46. memset(temp, 0, sizeof(temp)); // again, clearing all the values of temp for next iteration.
  47. }
  48.  
  49. for(int i = 0; i<r; i++){
  50. printf("Row %d's Max Pairwise GCD : %d\n", i+1, maximum_gcd[i]);
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Enter Row (R) and Column (C) : Enter all the numbers