-2

I'm trying to answer the following assignment:

We are given two arrays a and b containing elements each.

Choose a pair of elements (x,y) such that: x belongs to array a; y belongs to array b; gcd(x,y) is the maximum of all pairs (x,y).

If there is more than one such pair having maximum gcd, then choose the one with maximum sum.

Print the sum of elements of this maximum-sum pair.

n -> size of the input arrays

a and b are two arrays consisting of n numbers each.

This code is showing segmentation fault. I can't figure out the problem. it is showing segmentation fault for some test cases while for other it runs perfectly.Can you figure out the problem with the code?

#include <stdio.h>

long int max_gcd=0,max_sum=0;
long int n;
long int a[1000000],b[1000000];

long int hcf(long int n1, long int n2)
{
    if (n2 != 0)
        return hcf(n2, n1%n2);
    else
        return n1;
}
long int gcd(long int i,long int j,long int n)
{   
long int ans=hcf(a[i],b[j]);
    if(ans>max_gcd)
    {
        max_gcd=ans;
        max_sum=a[i]+b[j];
    }
    if(ans==max_gcd && max_sum<a[i]+b[j])
    {
        max_sum=a[i]+b[j];
    }
    if(j+1<n)
        return gcd(i,j+1,n);
    else if(i+1<n)
        return gcd(i+1,0,n);
    return max_sum;
}
int main()
{ 
    long int i;
    scanf("%ld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%ld",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        scanf("%ld",&b[i]);
    }
    long int ans=gcd(0,0,n);
    printf("%ld",ans);
    return 0;
}
ayushi09
  • 9
  • 3
  • What is this code supposed to do? Please read and follow [how to ask](https://stackoverflow.com/help/how-to-ask), thanks. –  Jul 18 '17 at 10:19
  • 2
    What is your input? – Jabberwocky Jul 18 '17 at 10:25
  • Probably array indexes out of bounds. You can check this easily yourself. – Jabberwocky Jul 18 '17 at 10:25
  • here we are given two arrays a and b containing elements each. we need to Choose a pair of elements(x,y) such that: x belongs to array a . y belongs to array b. gcd(x,y) is the maximum of all pairs(x,y) . If there is more than one such pair having maximum gcd, then choose the one with maximum sum. we need to Print the sum of elements of this maximum-sum pair. – ayushi09 Jul 18 '17 at 10:27
  • 3
    @coder09 don't post clarifications in comments but edit your question and put them _there_. And what are those particular test cases where the program fails?? – Jabberwocky Jul 18 '17 at 10:28
  • 1
    Can you use a debugger to pinpoint the issue? – Paul Floyd Jul 18 '17 at 11:06
  • @Marievi n declared Global in above section – EsmaeelE Jul 18 '17 at 12:24
  • 1
    You need to create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) & you should include the test cases where you get a segmentation fault. – Karim Manaouil Jul 18 '17 at 14:30
  • 1
    Please [edit] your code to reduce it to a [mcve] of your problem. Your current code requires unspecified input to run - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Jul 18 '17 at 17:04
  • @Marievi, `n` appears to be a global (line 4). – Toby Speight Jul 18 '17 at 17:05
  • it's an online coding competition question I guess....that's why test cases are hidden... – Aditya Jul 18 '17 at 18:03
  • The array just contain some random numbers all positive integers...given at runtime.... – Aditya Jul 18 '17 at 18:05
  • Look at [this](https://stackoverflow.com/questions/4885537/what-is-the-fastest-way-to-find-the-gcd-of-n-numbers) convert the answers to your need. – Yonlif Jul 18 '17 at 18:39

1 Answers1

1

You should not ask running contest's question. Better try it by own or wait for editorial. Don't abide contest rule. https://www.hackerrank.com/contests/w34/challenges/maximum-gcd-and-sum