Skip to content

Commit 2f8cd6f

Browse files
committed
Merge branch 'master' of github.com:knaxus/problem-solving-javascript
2 parents 7c8a66f + 878b928 commit 2f8cd6f

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { binarySearch, binarySearchRecursive } = require('.');
2+
3+
describe('Binary Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8];
5+
const low = 0;
6+
const high = array.length - 1;
7+
8+
describe('When element to find is at 1st position ', () => {
9+
it('Binary search with Loop', () => {
10+
expect(binarySearch(array, 1)).toEqual(0);
11+
});
12+
it('Binary serach with recursion', () => {
13+
expect(binarySearchRecursive(array, low, high, 1)).toEqual(0);
14+
});
15+
});
16+
describe('When element to find is at last position ', () => {
17+
it('Binary search with Loop', () => {
18+
expect(binarySearch(array, 7)).toEqual(6);
19+
});
20+
it('Binary serach with recursion', () => {
21+
expect(binarySearchRecursive(array, low, high, 7)).toEqual(6);
22+
});
23+
});
24+
describe('When element to find is at random position ', () => {
25+
it('Binary search with Loop', () => {
26+
expect(binarySearch(array, 3)).toEqual(2);
27+
});
28+
it('Binary serach with recursion', () => {
29+
expect(binarySearchRecursive(array, low, high, 3)).toEqual(2);
30+
});
31+
});
32+
describe('When element to find is no present in array ', () => {
33+
it('Binary search with Loop', () => {
34+
expect(binarySearch(array, 10)).toEqual(null);
35+
});
36+
it('Binary serach with recursion', () => {
37+
expect(binarySearchRecursive(array, low, high, 10)).toEqual(null);
38+
});
39+
});
40+
41+
});

src/Searching/BinarySearch/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ function binarySearchRecursive(arr, low, high, key) {
3333
return binarySearchRecursive(arr, mid + 1, high, key);
3434
}
3535
}
36+
37+
module.exports = {
38+
binarySearch,
39+
binarySearchRecursive,
40+
};

src/_Problems_/product-of-elements/index.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,41 @@ function findProduct(arr) {
2121
let right = 1;
2222

2323
// multiply all the numbers to the right side
24-
for (let i = arr.length - 1; i > 0; i -= 1) {
24+
for (let i = arr.length - 1; i >= 0; i -= 1) {
2525
result[i] *= right;
2626
right *= arr[i];
2727
}
2828
return result;
2929
}
30+
31+
function findProduct2(arr) {
32+
let countZeros = 0;
33+
let positionOfZero = -1;
34+
let productOffAllExpectZero = 1;
35+
let result = [];
36+
for (let i = 0; i < arr.length; i += 1) {
37+
if (arr[i] === 0) {
38+
countZeros += 1;
39+
positionOfZero = i;
40+
} else {
41+
productOffAllExpectZero *= arr[i];
42+
}
43+
}
44+
45+
if (countZeros === 0) {
46+
for (let i = 0; i < arr.length; i += 1) {
47+
result[i] = productOffAllExpectZero / arr[i];
48+
}
49+
} else if (countZeros === 1) {
50+
result = Array(arr.length).fill(0);
51+
result[positionOfZero] = productOffAllExpectZero;
52+
} else if (countZeros >= 2) {
53+
result = Array(arr.length).fill(0);
54+
}
55+
return result;
56+
}
57+
58+
module.exports = {
59+
findProduct,
60+
findProduct2,
61+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { findProduct, findProduct2 } = require('.');
2+
3+
describe('Productof elements', () => {
4+
let array = [];
5+
6+
describe('When count of zero is 0', () => {
7+
beforeEach(() => {
8+
array = [1, 2, 3, 4];
9+
});
10+
it('With 1st Approach', () => {
11+
expect(findProduct(array)).toEqual([24, 12, 8, 6]);
12+
});
13+
it('With 2st Approach', () => {
14+
expect(findProduct2(array)).toEqual([24, 12, 8, 6]);
15+
});
16+
});
17+
18+
describe('When count of zero is 1', () => {
19+
beforeEach(() => {
20+
array = [2, 3, 0, 4];
21+
});
22+
it('With 1st Approach', () => {
23+
expect(findProduct(array)).toEqual([0, 0, 24, 0]);
24+
});
25+
it('With 2st Approach', () => {
26+
expect(findProduct2(array)).toEqual([0, 0, 24, 0]);
27+
});
28+
});
29+
30+
describe('When count of zero is 2', () => {
31+
beforeEach(() => {
32+
array = [0, 3, 0, 4];
33+
});
34+
it('With 1st Approach', () => {
35+
expect(findProduct(array)).toEqual([0, 0, 0, 0]);
36+
});
37+
it('With 2st Approach', () => {
38+
expect(findProduct2(array)).toEqual([0, 0, 0, 0]);
39+
});
40+
});
41+
42+
describe('When count of zero is greater than 2', () => {
43+
beforeEach(() => {
44+
array = [0, 3, 0, 4, 8, 0];
45+
});
46+
it('With 1st Approach', () => {
47+
expect(findProduct(array)).toEqual([0, 0, 0, 0, 0, 0]);
48+
});
49+
it('With 2st Approach', () => {
50+
expect(findProduct2(array)).toEqual([0, 0, 0, 0, 0, 0]);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)