views:

1617

answers:

12

I was asked this question in a job interview, and I'd like to know how others would solve it. I'm most comfortable with Java, but solutions in other languages are welcome.

Given an array of numbers, nums, return an array of numbers products, where products[i] is the product of all nums[j], j != i.

Input : [1, 2, 3, 4, 5]
Output: [(2*3*4*5), (1*3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)]
      = [120, 60, 40, 30, 24]

You must do this in O(N) without using division.

+7  A: 

Here's my attempt to solve it in Java. Apologies for the non-standard formatting, but the code has a lot of duplication, and this is the best I can do to make it readable.

import java.util.Arrays;

public class Products {
    static int[] products(int... nums) {
        final int N = nums.length;
        int[] prods = new int[N];
        Arrays.fill(prods, 1);
        for (int
           i = 0, pi = 1    ,  j = N-1, pj = 1  ;
           (i < N)         && (j >= 0)          ;
           pi *= nums[i++]  ,  pj *= nums[j--]  )
        {
           prods[i] *= pi   ;  prods[j] *= pj   ;
        }
        return prods;
    }
    public static void main(String[] args) {
        System.out.println(
            Arrays.toString(products(1, 2, 3, 4, 5))
        ); // prints "[120, 60, 40, 30, 24]"
    }
}

The loop invariants are pi = nums[0] * nums[1] *.. nums[i-1] and pj = nums[N-1] * nums[N-2] *.. nums[j+1]. The i part on the left is the "prefix" logic, and the j part on the right is the "suffix" logic.


Recursive one-liner

Jasmeet gave a (beautiful!) recursive solution; I've turned it into this (hideous!) Java one-liner. It does in-place modification, with O(N) temporary space in the stack.

static int multiply(int[] nums, int p, int n) {
    return (n == nums.length) ? 1
      : nums[n] * (p = multiply(nums, nums[n] * (nums[n] = p), n + 1))
          + 0*(nums[n] *= p);
}

int[] arr = {1,2,3,4,5};
multiply(arr, 1, 0);
System.out.println(Arrays.toString(arr));
// prints "[120, 60, 40, 30, 24]"
polygenelubricants
I think the 2-variables loop makes it harder to understand than necessary (at least for my poor brain!), two separate loops would do the job as well.
Guillaume
That's why I separated the code into left/right, in an effort to show that the two are independent of each other. I'm not sure if that actually works, though =)
polygenelubricants
A: 

Well,this solution can be considered that of C/C++. Lets say we have an array "a" containing n elements like a[n],then the pseudo code would be as below.

for(j=0;j<n;j++)
  { 
    prod[j]=1;

    for (i=0;i<n;i++)
    {   
        if(i==j)
        continue;  
        else
        prod[j]=prod[j]*a[i];
  }
Vijay Sarathi
This is `O(N^2)` (but I didn't downvote).
polygenelubricants
A: 
    int[] arr1 = { 1, 2, 3, 4, 5 };
    int[] product = new int[arr1.Length];              

    for (int i = 0; i < arr1.Length; i++)
    {
        for (int j = 0; j < product.Length; j++)
        {
            if (i != j)
            {
                product[j] = product[j] == 0 ? arr1[i] : product[j] * arr1[i];
            }
        }
    }
isthatacode
This is O(n^2).
KennyTM
ok, was not sure of O(N) part, soes that mean using just a single loop ? thanks
isthatacode
@isthat - you can use multiple loops, but not NESTED loops.
mtrw
@isthatcode see this link: http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
Miguel Sevilla
+35  A: 

An explaination of polygenelubricants method is: The trick is to construct the arrays (in the case for 4 elements)

{              1,         a[0],    a[0]*a[1],    a[0]*a[1]*a[2],  }
{ a[1]*a[2]*a[3],    a[2]*a[3],         a[3],                 1,  }

Both of which can be done in O(n) by starting at the left and right edges respectively.

Then multiplying the two arrays element by element gives the required result

My code would look something like this:

int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N;++i)
{
  products_below[i]=p;
  p*=a[i];
}

int products_above[N];
p=1;
for(int i=N-1;i>=0;--i)
{
  products_above[i]=p;
  p*=a[i];
}

int products[N]; // This is the result
for(int i=0;i<N;++i)
{
  products[i]=products_below[i]*products_above[i];
}

If you need to be O(1) in space too you can do this (which is less clear IMHO)

int a[N] // This is the input
int products[N];

// Get the products below the curent index
p=1;
for(int i=0;i<N;++i)
{
  products[i]=p;
  p*=a[i];
}

// Get the products above the curent index
p=1;
for(int i=N-1;i>=0;--i)
{
  products[i]*=p;
  p*=a[i];
}
Michael Anderson
Yes, this is the prefix/suffix combo that I did; I'm just wondering if there's a simpler solution that I've missed.
polygenelubricants
This is O(n) runtime but it's also O(n) in space complexity. You can do it in O(1) space. I mean, other than the size of the input and output containers of course.
wilhelmtell
@wilhelmtell Agreed. Have added a simple modification of the algorithm to be O(1) in space.
Michael Anderson
Very clever! Is there a name for this algorithm?
fastcodejava
I don't know of a name for the algorithm, sorry.
Michael Anderson
+1  A: 

Tricky:

Use the following:

public int[] calc(int[] params) {

int[] left = new int[n-1]
in[] right = new int[n-1]

int fac1 = 1;
int fac2 = 1;
for( int i=0; i<n; i++ ) {
    fac1 = fac1 * params[i];
    fac2 = fac2 * params[n-i];
    left[i] = fac1;
    right[i] = fac2; 
}
fac = 1;

int[] results = new int[n];
for( int i=0; i<n; i++ ) {
    results[i] = left[i] * right[i];
}

Yes, I am sure i missed some i-1 instead of i, but thats the way to solve it.

Daniel Migowski
+4  A: 

C++, O(n):

long long prod = accumulate(in.begin(), in.end(), 1LL, multiplies<int>());
transform(in.begin(), in.end(), back_inserter(res),
          bind1st(divides<long long>(), prod));
wilhelmtell
division is not allowed
Michael Anderson
That's still an awesome looking code, though. With disclaimer that it uses division, I'd still upvote if given explanation.
polygenelubricants
Damn, I didn't read the question through. :s @polygenelubricants explanation: the idea is to do it in two steps. First take the factorial of the first sequence of numbers. That's what the accumulate algorithm does (by default adds numbers, but can take any other binary operation to replace the addition, in this case a multiplication). Next I iterated over the input sequence a second time, transforming it such that the corresponding element in the output sequence the factorial I calculated in the previous step divided by the corresponding element in the input sequence.
wilhelmtell
"factorial of the first sequence"? wtf? i meant the product of the sequence elements.
wilhelmtell
+1  A: 

This is O(n^2) but f# is soooo beautiful:

List.fold (fun seed i -> List.mapi (fun j x -> if i=j+1 then x else x*i) seed) 
          [1;1;1;1;1]
          [1..5]
Lars
+11  A: 

Translating Michael Anderson's solution into Haskell:

otherProducts xs = zipWith (*) below above

     where below = scanl (*) 1 $ init xs

           above = tail $ scanr (*) 1 xs
FredOverflow
+6  A: 

Here is a small recursive function (in C++) to do the modofication in place. It requires O(n) extra space (on stack) though. Assuming the array is in a and N holds the array length, we have

int multiply(int *a, int fwdProduct, int indx) {
    int revProduct = 1;
    if (indx < N) {
       revProduct = multiply(a, fwdProduct*a[indx], indx+1);
       int cur = a[indx];
       a[indx] = fwdProduct * revProduct;
       revProduct *= cur;
    }
    return revProduct;
}
Jasmeet
OMG! I've never seen such a beautiful recursive technique! +1!
polygenelubricants
@polygenelubricants, Thanks
Jasmeet
+5  A: 

Sneakily circumventing the "no divisions" rule:

sum = 0.0
for i in range(a):
  sum += log(a[i])

for i in range(a):
  output[i] = exp(sum - log(a[i]))
sth
@sth: so sneaky!
slf
Nitpick: as far as I'm aware, computers implement logarithms using their binomial expansion - which *does* require division...
Mark Bannister
That would result in floating point output though...
Alderath
A: 

There also is a O(N^(3/2)) non-optimal solution. It is quite interesting, though.

First preprocess each partial multiplications of size N^0.5(this is done in O(N) time complexity). Then, calculation for each number's other-values'-multiple can be done in 2*O(N^0.5) time(why? because you only need to multiple the last elements of other ((N^0.5) - 1) numbers, and multiply the result with ((N^0.5) - 1) numbers that belong to the group of the current number). Doing this for each number, one can get O(N^(3/2)) time.

Example:

4 6 7 2 3 1 9 5 8

partial results: 4*6*7 = 168 2*3*1 = 6 9*5*8 = 360

To calculate the value of 3, one needs to multiply the other groups' values 168*360, and then with 2*1.

kolistivra
A: 

One more solution, Using division. with twice traversal. Multiply all the elements and then start dividing it by each element.

alam