# O(n) solution to Multiply Others problem in F#

Yan Cui

I help clients go faster for less using serverless technologies.

The real-time data platform that empowers developers to build innovative products faster and more reliably than ever before.

Another tasty challenge that I ran into during my preparation for technical interviews is this seemingly simple problem from Facebook.

input [2,3,1,4]
output [12,8,24,6]

Multiply all fields except it’s own position.

Restrictions:
1. no use of division
2. complexity in O(n)

The main challenge is in making the algorithm run in O(n) time.

One solution I saw (and thought it was quite clever) is to maintain two temporary arrays, calculated by multiplying the elements of the input array from front-to-back, and back-to-front.

ie. input is [ 2, 3, 1, 4 ]

The front array starts with [ 1, 0, 0, 0 ], and starting from position 1, we can say front[i] = front[i-1] * input[i-1] and end up with [ 1, 2, 6, 6 ] (which is the product of all the input elements before that position.

The rear array starts with [ 0, 0, 0, 1 ] and starting from position 2 (ie, second to last), we can say rear[i] = rear[i+1] * input[i+1] and end up with [ 12, 4, 4, 1 ].

And finally we can work out the output array by multiplying the corresponding elements in the front and rear arrays, and that runs in O(n) of time and space.

Here’s the implementation in F#.

## Try it Yourself

1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

### 10 thoughts on “O(n) solution to Multiply Others problem in F#”

1. :)

let multOthers : int list -> int list =
fun xs ->
let rec hlp n = function
| [ ] | [_] -> [ ] | x :: xs -> let i = x*n in i :: (hlp i xs)
(1 :: xs |> hlp 1,
1 :: xs |> List.rev |> hlp 1 |> List.rev)
||> List.zip
|> List.map(fun (x,y) -> x*y)

[ 2; 3; 1; 4; ] |> multOthers

(it probably doesn’t look nice cos of no monospace font)

2. ``` let multOthers : int list -> int list = fun xs -> let rec hlp n = function | [ ] | [_] -> [ ] | x :: xs -> let i = x*n in i :: (hlp i xs) (1 :: xs |> hlp 1, 1 :: xs |> List.rev |> hlp 1 |> List.rev) ||> List.zip |> List.map(fun (x,y) -> x*y)```

``` ```

```[ 2; 3; 1; 4; ] |> multOthers ```

3. Yeah, my bad, forgot the ()

``` ... (1 :: xs |> hlp 1, 1 :: (xs |> List.rev) |> hlp 1 |> List.rev) ... ```

4. Yeah, my bad, forgot ()
``` ... (1 :: xs |> hlp 1, 1 :: (xs |> List.rev) |> hlp 1 |> List.rev) ... ```

5. Hi Yan. Your site brings my browser to a crashing halt with all of the ads. By disabling Flash, I was able to make the page stable enough to leave this comment, but it’s still using loads of CPU. I believe it’s pretty much unusable on mobile for this reason. Would you consider removing some ads and/or widgets?

6. Here’s one using effectively the same algorithm but with more standard library functions and no mutation:

``` let multiplyOthers xs = let front = Array.scan (*) 1 xs let back = Array.scanBack (*) xs 1 Array.map2 (*) front.[..front.Length - 2] back.[1..] ```