Re: [algogeeks] Re: a google question
@Gene I think there is some mistake in this programe. when the input is : a[ ] = {30,25,19,16} b[ ] = {20,18,14,10} the right result should be 30+20, 30+18, 25+20,30+14 but the output of your programe is 30+20,30+18,25+20,25+18 Best Regards! 2010/5/4 Gene gene.ress...@gmail.com I propose this solution (it's C89): #include stdio.h //int a[] = { 8, 7, 4, 3, 2, 1, 1, 1, 1, 1 }; //int b[] = { 34, 23, 21, 19, 15, 13, 11, 8, 4, 2 }; int a[] = { 6, 5, 4, 3, 2, 1 }; int b[] = { 9, 8, 6, 5, 3, 2 }; void find_pairs(int *a, int *b, int n) { int iamax[100], ibmax[100], i, ia, ib; for (i = 0; i n; i++) iamax[i] = ibmax[i] = -1; ia = ib = 0; for (i = 0; i n; i++) { printf((%d,%d)\n, a[ia], b[ib]); if (a[ia + 1] + b[ibmax[ia + 1] + 1] b[ib + 1] + a[iamax[ib + 1] + 1]) ib = ++ibmax[++ia]; else ia = ++iamax[++ib]; } } int main(void) { find_pairs(a, b, sizeof a / sizeof a[0]); return 0; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] a google question
@Kushwaha Your programe is wrong. Try this input: a[ ] = {30,25,19,16,14}; b[ ] = {20,18,12,10,8}; the right answer should be 50 48 45 43 42 but the program output is 50 48 45 43 37。 2010/5/2 Jitendra Kushwaha jitendra.th...@gmail.com Here is a solution of O(n) , taking 4 pointers 2 for each array #include cstdio #includeiostream 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;iN;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++ ; elseans = 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 algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.