On May 3, 9:43 am, Jitendra Kushwaha <jitendra.th...@gmail.com> wrote: > @divya > > I try to simulate what you said for the given array > > index : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 > array1 : 8, 7, 4 ,3 , 2, 1, 1, 1, 1, 1 > ^ ^ > p11 p12 > > *p11 = 8 and *p12 = 3 > > index : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 > array2 : 34, 23, 21, 19, 15, 13, 11, 8, 4, 2 > ^ ^ > p21 p22 > > *p21 = 34 and *p22 = 23 > > a = 8 + 34 = 41 //arr1[0] + arr2[0] > b = 8 +23 = 31 //arr1[0] + arr2[1] > c = 34 + 3 = 37 //arr1[4] + arr2[0] greatest !!! > d = 7 +23 = 30 //arr1[1] + arr2[1] > > arr1[0] + arr2[2] = 29 which is less than c.. > > here is my output > > arr1[0] + arr2[0] = 42 > arr1[1] + arr2[0] = 41 > arr1[2] + arr2[0] = 38 > arr1[3] + arr2[0] = 37 > arr1[4] + arr2[0] = 36 > arr1[5] + arr2[0] = 35 > arr1[6] + arr2[0] = 35 > arr1[7] + arr2[0] = 35 > arr1[8] + arr2[0] = 35 > arr1[9] + arr2[0] = 35 > > i have attached the code try with replacing arr1 and arr2 with > following.and the case u wanted to point out will be taken throught in > following test case..(arr1[0] + arr2[1] = 9+27 = 36 will be taken before > arr1[6] + arr2[0] = 1+34 = 35 ) > > int arr1[N] = {9,7,4,3,2,1,1,1,1,1}; > int arr2[N] = {34,27,21,19,15,13,11,8,4,2}; > > Regards > Jitendra Kushwaha > > -- > 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 > athttp://groups.google.com/group/algogeeks?hl=en.
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.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.