@ karthikeyan ..  you're using O(n) space ..

Try do it in O(n) time with O(1) space ...
Array sorted or not doesn't matter !!!

~raju

On Thu, Sep 29, 2011 at 9:20 PM, KARTHIKEYAN V.B. <algo...@gmail.com> wrote:

>
> //use dynamic programming approach
>
> //it is O(n) time and O(1) space
>
> #include<stdio.h>
> #define N 5
>
> void main()
> {
> int a[N]={1,2,3,4,5},i;
> int prod1[N];
> int p=1;
> for(i=0;i<N;++i)
> {
>   prod1[i]=p;
>   p*=a[i];
> }
>
> int prod2[N];
> p=1;
> for(i=N-1;i>=0;--i)
> {
>   prod2[i]=p;
>   p*=a[i];
> }
>
> int products[N];
> for(i=0;i<N;++i)
> {
>   products[i]=prod1[i]*prod2[i];
>   printf("%d ",products[i]);
> }
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@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.

Reply via email to