Skip to content

Commit 754fa31

Browse files
committed
--update: find product of elements
1 parent 7a3ce7c commit 754fa31

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Searching/BinarySearch/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,4 @@ function binarySearchRecursive(arr, low, high, key) {
3232
} else if (key > arr[mid]) {
3333
return binarySearchRecursive(arr, mid + 1, high, key);
3434
}
35-
36-
return null;
3735
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// An array such that each index has a product of all the numbers in the array except the number stored at that index.
2+
3+
function findProduct(arr) {
4+
let left = 1;
5+
const result = [];
6+
7+
// multiply all the numbers to the left side
8+
for (let el of arr) {
9+
result.push(left);
10+
left *= el;
11+
}
12+
13+
let right = 1;
14+
15+
// multiply all the numbers to the right side
16+
for (let i = arr.length - 1; i > 0; i -= 1) {
17+
result[i] *= right;
18+
right *= arr[i];
19+
}
20+
return result;
21+
}

0 commit comments

Comments
 (0)