Skip to content

Commit 9b3e14c

Browse files
committed
JS solution and test for DCP #2
1 parent 5167a18 commit 9b3e14c

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Problem #2 [Hard]
2+
3+
## Problem Description
4+
5+
> Given an array of integers, return a new array such that each element at index `i` of the new array is the product of all the numbers in the original array except the one at `i`.
6+
7+
## Implementation
8+
9+
My solution generates the result array by sweeping through the source array twice, which means it falls under time complexity of `O(n)`, where `n` is the number of elements in the source array.
10+
11+
- JavaScript
12+
- [Solution](./productOfAllOthers.js)
13+
- [Jest Test File](./productOfAllOthers.test.js)
14+
- [CodeSandbox](https://codesandbox.io/s/586mk36lx?autoresize=1&fontsize=14&module=%2FproductOfAllOthers.js&previewwindow=tests)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Given an array of integers, returns a new array such that each element at index i
3+
* of the new array is the product of all the numbers in the original array
4+
* except the one at i.
5+
* @param {Number[]} arr An array of integers.
6+
* @return {Number[]} The resulting array.
7+
*/
8+
function productOfAllOthers(arr) {
9+
// Reduce the elements of source array into the total product of all elements
10+
const totalProduct = arr.reduce(
11+
(runningProduct, currentVal) => (runningProduct *= currentVal),
12+
1
13+
);
14+
15+
// Map over the source array to return a result array where each
16+
// resulting element is the totalProduct divided by the original element.
17+
return arr.map(num => totalProduct / num);
18+
}
19+
20+
// Naive brute-force approach
21+
function bruteForceProductOfAllOthers(arr) {
22+
const result = [];
23+
for (let i = 0; i < arr.length; i++) {
24+
let product = 1;
25+
for (let j = 0; j < arr.length; j++) {
26+
if (arr[j] !== arr[i]) {
27+
product *= arr[j];
28+
}
29+
}
30+
result.push(product);
31+
}
32+
return result;
33+
}
34+
35+
export default productOfAllOthers;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import productOfAllOthers from "./productOfAllOthers";
2+
3+
test(`[1, 2, 3, 4, 5] returns [120, 60, 40, 30, 24]`, () => {
4+
expect(productOfAllOthers([1, 2, 3, 4, 5])).toEqual([120, 60, 40, 30, 24]);
5+
});
6+
7+
test(`[3, 2, 1] returns [2, 3, 6]`, () => {
8+
expect(productOfAllOthers([3, 2, 1])).toEqual([2, 3, 6]);
9+
});

Daily-Coding-Problem/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Daily Coding Problem
2+
3+
Solutions to problems received from [dailycodingproblem.com](https://www.dailycodingproblem.com)
4+
5+
- [2. Product of All Others](./2-Product-of-All-Others/README.md)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ The following problems are introduced in the [Interview Cake full course](https:
6969
- [42. Find Duplicate Files](./42-Find-Duplicate-Files/README.md)
7070

7171
- [43. Merge Sorted Arrays](./43-Merge-Sorted-Arrays/README.md)
72+
73+
## Daily Coding Problem
74+
75+
- [2. Product of All Others](./Daily-Coding-Problem/2-Product-of-All-Others/README.md)

0 commit comments

Comments
 (0)