I am trying to find the GCD/HCF of an array, I know to write the function that finds the GCD of two numbers using Euclid's algorithm. so to find the GCD of the array I thought to use this Euclid algorithm as a divide and conquer technique for GCD arrays. I'm successfully able to divide it but stuck with the merge function to again do the GCD operation, I'm looking for help for the merge function in such cases i.e conquer part.
my code for it is as;
#include <iostream>
using namespace std;
long long GCD(long long m,long long n)
{
while(m%n!=0)
{
long long next_m=n;
long long next_n=m%n;
m=next_m;
n=next_n;
}
return n;
}
//define merge function.
long long hcf_arr(long long *arr,long long start,long long end){
if(end-start+1==2){
return GCD(arr[start],arr[end]);
}
else{
long long *u;
u=new long long[(end-start+1)/2];
long long *v;
v=new long long[(end-start+1)-(end-start+1)/2];
for(long long i=start;i<=(end-start+1)/2;i++){
u[i]=arr[i];
}
for(long long i=(end-start+1)/2+1;i<=(end-start+1);i++){
v[i]=arr[i];
}
hcf_arr(u,start,(end-start+1)/2);
hcf_arr(v,(end-start+1)/2+1,end-start+1);
//Merge function
}
}
int main() {
}