@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 [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.
#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;
int w=0,x=0,y=0,z=0;
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 , printf(" arr1[%d] + arr2[%d] = ",w,y),x++,z++;
else if(b >= c && b >= d ) ans = b , p22++ , printf(" arr1[%d] + arr2[%d] = ",w,z),z++;
else if(c >= b && c >= d ) ans = c , p12++ , printf(" arr1[%d] + arr2[%d] = ",x,y),x++;
else ans = d , p11++ , p21++ , printf(" arr1[%d] + arr2[%d] = ",w,y),w++,y++;
printf("%d\n",ans);
}
}