Skip to content

Commit ce00e13

Browse files
author
Hridyanshu7
committed
fix: optimized AverageMean.js by removing redundant comments and unnecessary operations.
1 parent ff314a2 commit ce00e13

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

Diff for: Recursive/SubsetSum.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Problem Statement: Given an array of numbers and a target sum, find the number of distinct subsets that sums up to the target.
3+
*
4+
* What is a subset?
5+
* A subset is a selection of elements from an array where the order of elements remains unchanged. A subset can include any combination of elements, including no elements at all (i.e., the empty set).
6+
* Example: Given an array = [1, 2]
7+
* 1. [] is a subset (empty set)
8+
* 2. [1] is a subset
9+
* 3. [2] is a subset
10+
* 4. [1, 2] is a subset
11+
*
12+
* How does the number of subsets relate to the array size?
13+
* An array of size k has 2^k possible subsets.
14+
* Example: For an array = [10, 5], the possible subsets are [], [10], [5], [10, 5].
15+
*
16+
* Problem Example:
17+
* 1. I/P: arr = [10, 5, 2, 3, 6], sum = 8
18+
* O/P: 2 (The subsets [2, 6] and [5, 3] both sum to 8)
19+
*
20+
* 2. I/P: arr = [-1, -1, -1], sum = -3
21+
* O/P: 1 (The subset [-1, -1, -1] sums to -3)
22+
*
23+
* 3. I/P: arr = [40, 9, 77], sum = 3
24+
* O/P: 0 (No subset sums to 3)
25+
*
26+
* Algorithm:
27+
* Recursively explore all subsets, either including or excluding each element of the array, here inclusion means subtracting the sum by the element included, finally check if sum equals zero, this would indicate that the sum of all the elements included is equal to the target sum.
28+
*
29+
*/
30+
31+
/**
32+
* @function subsetSum
33+
* @description This function recursively calculates the count of subsets whose sum equals the given target sum.
34+
* @param {number[]} arr - The input array of numbers.
35+
* @param {number} sum - The target sum we want to find in the subsets.
36+
* @param {number} ind - The current index.
37+
* @return {number} The count of subsets whose sum equals the target sum.
38+
*
39+
*/
40+
41+
export function subsetSum(arr, sum, ind = 0) {
42+
//input validation only in the inital call
43+
if (
44+
ind === 0 &&
45+
(!Array.isArray(arr) || !arr.every((elem) => typeof elem === 'number'))
46+
) {
47+
throw new TypeError('arr should be an array of numbers')
48+
}
49+
50+
if (ind === 0 && typeof sum !== 'number') {
51+
throw new TypeError('sum should be a number')
52+
}
53+
54+
if (ind === arr.length) {
55+
return sum === 0 ? 1 : 0
56+
}
57+
58+
return subsetSum(arr, sum, ind + 1) + subsetSum(arr, sum - arr[ind], ind + 1)
59+
}

Diff for: Recursive/test/SubsetSum.test.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { subsetSum } from '../SubsetSum'
2+
3+
const tests = [
4+
{
5+
test: {
6+
arr: [10, 5, 2, 3, 6],
7+
sum: 8
8+
},
9+
expectedValue: 2
10+
},
11+
{
12+
test: {
13+
arr: [-1, -1, -1],
14+
sum: -3
15+
},
16+
expectedValue: 1
17+
},
18+
{
19+
test: {
20+
arr: [40, 9, 77],
21+
sum: 3
22+
},
23+
expectedValue: 0
24+
}
25+
]
26+
27+
describe('SubsetSum', () => {
28+
test.each(tests)(
29+
'should return $expectedValue when input is $test.arr and sum is $test.sum',
30+
({ test, expectedValue }) => {
31+
expect(subsetSum(test.arr, test.sum)).toBe(expectedValue)
32+
}
33+
)
34+
35+
//Empty array cases
36+
it('should return 1 when input is an empty array and sum is 0', () => {
37+
const result = subsetSum([], 0)
38+
expect(result).toBe(1) // Empty subset ([]) sums to 0
39+
})
40+
41+
it('should return 0 when input is an empty array and sum is not 0', () => {
42+
const result = subsetSum([], 5)
43+
expect(result).toBe(0) // No subsets available to sum to 5
44+
})
45+
46+
// Test invalid cases for errors
47+
describe('Invalid input cases', () => {
48+
it('should throw a TypeError when arr is not an array', () => {
49+
expect(() => subsetSum('invalid array', 5)).toThrow(TypeError)
50+
})
51+
52+
it('should throw a TypeError when arr contains non-number elements', () => {
53+
expect(() => subsetSum([1, 2, 'three', 4], 5)).toThrow(TypeError)
54+
})
55+
56+
it('should throw a TypeError when sum is not a number', () => {
57+
expect(() => subsetSum([1, 2, 3], 'five')).toThrow(TypeError)
58+
})
59+
})
60+
61+
// Edge case
62+
it('should handle large arrays correctly', () => {
63+
const largeArray = Array.from({ length: 20 }, (_, i) => i + 1) // [1, 2, ..., 20]
64+
const result = subsetSum(largeArray, 10)
65+
expect(result).toBeGreaterThan(0) // Ensure this works for large inputs
66+
})
67+
})

0 commit comments

Comments
 (0)