Here is a solution of O(n) , taking 4 pointers 2 for each array
#include <cstdio>
#include<iostream>
using namespace std;
#define N 10
int main(void)
{
int arr1[N] = {8,7,4,3,2,1,1,1,1,1};
int arr2[N] = {34,23,21,19,15,13,11,8,4,2};
int *p11,*p12,*p21,*p22;
p11 = p12 = arr1;
p21 = p22 = arr2;
int f1;
f1 = 0;
for(int i=0;i<N;i++) {
int ans=0;
int a,b,c,d;
a = *p11 + *p21;
b = *p11 + *p22;
c = *p21 + *p12;
d = *(p11+1) + *(p21+1);
//printf("a=%d b=%d c=%d d=%d\n",a,b,c,d); //debug
if(f1==0) ans = a , p12++ , p22++ , f1=1;
else if(b >= c && b >= d ) ans = b , p22++ ;
else if(c >= b && c >= d ) ans = c , p12++ ;
else ans = d , p11++ , p21++ ,printf("4 ");
printf("%d\n",ans);
}
}
Regards
Jitendra Kushwaha
Undergradute Student
Computer Science & Eng.
MNNIT, Allahabad
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.