Given a list A with n elements, produce a list B with n elements such that the ith element of B is equal to the product of all elements except the ith in list A. Example: Given list A = [1, 2, 3], make a function f(x) such that f(A) = [6, 3, 2].
foo(int* a,int len)
{
int prod=1;
int* b=new int[len];
for(int i=len-1;i>=0;i++)
{
b[i]=prod;
prod=prod*a[i];
}
prod=1;
for(int i=0;i < len;i++)
{
b[i]=b[i]*prod;
printf("%d\n",b[i]);
prod=prod*a[i];
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment