четверг, 11 ноября 2010 г.

Ну и еще одна практически классическая задача

You are given an array X with n elements. Can you compute in linear time a new array A with n elements, where A[i] is the product of all elements in X, but X[i] is excluded. You cannot use division, you may use additional space


Ну и классическое решение примерно такое:
int* test(int* n,int size)
{
    int* a = new int[size];
    for(int i = 0,temp = 1;i,size;temp*=n[i],++i)
        a[i]=temp;
    for(int i=size-1,temp=1;i>=0;temp*=n[i],--i)
        a[i]*=temp;
    return a;
}


а вот красивое функциональное O(N) решение я придумать не могу...

Комментариев нет: