You are given two sorted integer arrays, which have some common integers. Any common integer between the two sequences constitute an intersection point.
You can start traversing from any of the array and switch to the other array or continue with the same array at an intersection point.
The objective is to find a path that produces the maximum sum of the data.
Take for example the following two sequences where intersection points are printed in bold:
First = 3 5 7 9 20 25 30 40 55 56 57 60 62
Second = 1 4 7 11 14 25 44 47 55 57 100
In the above example, the largest possible sum is 450 which is the result of adding
3, 5, 7, 9, 20, 25, 44, 47, 55, 56, 57, 60, and 62
I wrote the following code but it is not compiling, please help:
/* M: size of the array a
N: size of the array b
*/
int i,j,sum1,sum2,sum,m_i,n_j = 0;
void printIntersectionElements(int *a,int M, int *b, int N) {
if (M == 0)
return (b);
if (N ==0)
return(a);
while( i < M && j < N ){
sum1 = sum1 +a[i];
sum2 = sum2 + b[j];
if(a[i] == b[j]) { // found a common element.
if(sum1>= sum2){
for(;m_i<= i; m_i++)
cout<< a[m_i];
sum = sum +sum1;
m_i = i+1;
}
else {
for(;n_j<= j; n_j++)
cout<< b[n_j];
sum = sum+sum2;
n_j = j+1;
}
sum1 = sum2 = 0;
}
i++;
j++;
}
}
You are trying to return a result from a function that is declared with a
voidreturn type. That’s not valid C++, which is why you are getting a compiler error.There may be other errors, too. Read the error messages: they will tell you exactly what and where the problem is.