LEDIV(Little elephant and divisor)-runtime error

#include<stdio.h>

#include<stdlib.h>

int min(int,int);

int min_prime(int);

int gcd(int,int);

int main(){

int i,j,t,g;
int n=0;
int *a;        
scanf("%d\n",&t);

while(t-- > 0){
              scanf("%d\n",&n);
              a=(int*)malloc(sizeof(int)*(n));                  
              for(i=0;i<n;i++){
                scanf("%d",a+i);
              }
              g=a[0];
              for(i=1;i<n;i++){
                           g=gcd(g,a[i]);     
              }
             // printf("The gcd %d\n",g);
              if(g==1)
                      printf("%d\n",-1);
              else
                      printf("%d\n",min_prime(g));
              free(a);
}


//getch();
return 0;

}
int min(int a,int b){

if(a>b)
       return b;
else   
       return a;

}

int gcd(int a,int b){

if(b==0)
        return a; 
else
    a=gcd(b,a%b);

//printf("The gcd:%d",a);
return a;

}

int min_prime(int n){

int i=0,j;
int *a=(int*)malloc(sizeof(int)*(n+1));
for(i=2;i<=n;i++){
  a[i]=i;
}
for(i=2;pow(i,2)<=n ;i++){
  if(a[i]==i){
              for(j=2*i;j<=n;j=j+i){
                      a[j]=min(a[j],i);
                     
              }
  }
}
//free(a);

return a[n];

}

You have not defined any header file for getch(). Dont use it ,no need to use it. Moreover in int min_prime(int n) function in last two lines you are freeing the memory before returning. once you free the memory how can you return it ?

i have made the changes u ask.still getting error

can you provide question link

@baljit plz check it out.removed getch() as it is not allowed in GCC.u plz do run it and help me out

@aniruddha_paul There is a problem in your min_prime() function. probably you are trying to access an unallocated memory location that’s why you are getting runtime error.
i have replaced your min_prime() with another min_prime() code in the below link , it work’s well.

if you did’nt understood this min_prime() then just see this Efficient program to print all prime factors of a given number

Thankx man.I am new to programming.I m sometimes stuck while coding with c.its great that you helped me out.