wu :: forums
« wu :: forums - Products of other elements »

Welcome, Guest. Please Login or Register.
May 16th, 2024, 6:20am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, Grimbal, SMQ, ThudnBlunder, Eigenray, william wu, towr)
   Products of other elements
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Products of other elements  (Read 1309 times)
william wu
wu::riddles Administrator
*****





   
WWW

Gender: male
Posts: 1291
Products of other elements  
« on: Jun 20th, 2011, 7:18pm »
Quote Quote Modify Modify

Problem: You are given an array of numbers X = { x1, x2, x3, ..., xN }.
 
Without using division or logarithms, design an algorithm that produces the array of products

{ x2*x3*...*xN , x1*x3*...*xN, x1*x2*x4*....*xN, ... }

In other words, the ith product is the product of all numbers except the ith.
 
[06-21-2010: edited to clarify that logs aren't allowed -- thanks towr]
« Last Edit: Jun 21st, 2011, 7:48am by william wu » IP Logged


[ wu ] : http://wuriddles.com / http://forums.wuriddles.com
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Products of other elements  
« Reply #1 on: Jun 20th, 2011, 10:21pm »
Quote Quote Modify Modify

Can we use logs, subtraction and exponents?  Tongue
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Products of other elements  
« Reply #2 on: Jun 21st, 2011, 5:57am »
Quote Quote Modify Modify


Multiply the arrays
    [ 1, x1, x1*x2, ..., x1*...*xN-1 ]
and
    [ x2*...*xN, x3*...*xN, ..., xN, 1]
both of which are easy to compute in O(N).
IP Logged
william wu
wu::riddles Administrator
*****





   
WWW

Gender: male
Posts: 1291
Re: Products of other elements  
« Reply #3 on: Jun 21st, 2011, 7:49am »
Quote Quote Modify Modify

Grimbal: Yup that was the answer I had in mind Smiley
IP Logged


[ wu ] : http://wuriddles.com / http://forums.wuriddles.com
JohanC
Senior Riddler
****





   


Posts: 460
Re: Products of other elements  
« Reply #4 on: Jun 22nd, 2011, 3:22am »
Quote Quote Modify Modify

Do I understand it correctly that if you reuse the input to store the output, you need one extra array for temporary results?
And that only if you resort to O(n2) calculations you can get rid of that need for extra space?
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Products of other elements  
« Reply #5 on: Jun 22nd, 2011, 8:38am »
Quote Quote Modify Modify

Actually, you don't need to destroy the input nor to use an intermediate array.
 
x[] = input
y[] = new double[x.length];
double prod = 1;
for( i=0 ; i<x.length ; i++ ){
    y[i] = prod;
    prod *= x[i];
}
prod = 1;
for( i=x.length ; i-->0 ; ){
    y[i] *= prod;
    prod *= x[i];
}

y[] = tadaaaa!
 
PS: But you are right, I wouldn't know how to do it all in one array, with no division.
« Last Edit: Jun 22nd, 2011, 8:41am by Grimbal » IP Logged
william wu
wu::riddles Administrator
*****





   
WWW

Gender: male
Posts: 1291
Re: Products of other elements  
« Reply #6 on: Jul 23rd, 2011, 10:41pm »
Quote Quote Modify Modify

It can also be done in constant space, but I do not know what the optimal runtime is in that case. My solution for the constant space case also runs in O(n^2).
IP Logged


[ wu ] : http://wuriddles.com / http://forums.wuriddles.com
agent_purge
Newbie
*





   


Posts: 4
Re: Products of other elements  
« Reply #7 on: Jul 26th, 2011, 3:07am »
Quote Quote Modify Modify

Divide and conquer recursive algo using log(n) space and n*log(n) time
 
hidden:

product(start, end)
{
   if(start == end)
   {
 p = x[start];
 x[start] = 1;
 return p;
   }
 
   mid = (start+end)/2;
   p1 = product(start, mid);
   p2 = product(mid+1, end);
   
   for(i=1; i<=mid; i++)
   {
 x[i] *= p2;
   }
   
   for(i=mid+1; i<=end; i++)
   {
 x[i] *= p1;
   }
 
   return p1*p2;
}
IP Logged
william wu
wu::riddles Administrator
*****





   
WWW

Gender: male
Posts: 1291
Re: Products of other elements  
« Reply #8 on: Nov 8th, 2011, 1:38pm »
Quote Quote Modify Modify

Here's my solution for constant space and quadratic time. It uses two additional registers t1 and t2.
 

#include <stdio.h>
main(void) {
 int a[] = {3,5,1,2,7}; // input array
 int n = sizeof(a)/sizeof(int);
 int t1=1, t2=1; // temp variables
 int j,k;  // iterators
 for (k=0;k<n-1;k++) {
  t2 = a[k];
  a[k] = a[k+1];
  for (j=k+2;j<=n-1;j++) a[k] = a[k]*a[j];
  a[k] = a[k]*t1;
  t1 = t1*t2;
 }
 a[n-1] = t1;  
 for (k=0;k<n;k++) printf("%d ",a[k]); // print output
 // output: 70 42 210 105 30  
}
« Last Edit: Nov 8th, 2011, 1:39pm by william wu » IP Logged


[ wu ] : http://wuriddles.com / http://forums.wuriddles.com
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board