this ques was asked by google.. it could find any gud soln than order n*n
On Mon, Jul 19, 2010 at 10:55 AM, 王奇凡 <[email protected]> wrote:
> @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 <[email protected]>
>
> 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]<algogeeks%[email protected]>
>> .
>> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
With Regards
Ankur Aggarwal
--
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.