mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
feat: added find subsets algorithm using bitmanipulation (#1514)
* feat: added find subsets algorithm using bitmanipulation * file name fix * test file name fix * fix: codespell fix * error handled * added test cases for error
This commit is contained in:
34
Bit-Manipulation/GenerateSubSets.js
Normal file
34
Bit-Manipulation/GenerateSubSets.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* @function generateSubSets
|
||||||
|
* @param {Array} inputArray
|
||||||
|
* @returns {Array}
|
||||||
|
* @example [1,2] -> [[],[1],[2],[1,2]]
|
||||||
|
*/
|
||||||
|
|
||||||
|
// The time complexity of this algorithm is BigO(2^n) where n is the length of array
|
||||||
|
function generateSubSets(inputArray) {
|
||||||
|
if (!Array.isArray(inputArray)) {
|
||||||
|
throw new Error('Provided input is not an array')
|
||||||
|
}
|
||||||
|
if (inputArray.length > 32) {
|
||||||
|
throw new RangeError('Error size should be less than equal to 32')
|
||||||
|
}
|
||||||
|
let arrayLength = inputArray.length
|
||||||
|
let subSets = []
|
||||||
|
// loop till (2^n) - 1
|
||||||
|
for (let i = 0; i < 1 << arrayLength; i++) {
|
||||||
|
let subSet = []
|
||||||
|
for (let j = 0; j < arrayLength; j++) {
|
||||||
|
// 1 << j it shifts binary digit 1 by j positions and then we perform
|
||||||
|
// and by AND operation we are checking whetheer jth bit
|
||||||
|
// in i is set to 1 if result is non zero just add into set
|
||||||
|
if (i & (1 << j)) {
|
||||||
|
subSet.push(inputArray[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subSets.push(subSet)
|
||||||
|
}
|
||||||
|
return subSets
|
||||||
|
}
|
||||||
|
|
||||||
|
export { generateSubSets }
|
36
Bit-Manipulation/test/GenerateSubSets.test.js
Normal file
36
Bit-Manipulation/test/GenerateSubSets.test.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { generateSubSets } from '../GenerateSubSets'
|
||||||
|
|
||||||
|
describe('subSets', () => {
|
||||||
|
it('find the subsets', () => {
|
||||||
|
expect(generateSubSets([1, 2, 3])).toEqual([
|
||||||
|
[],
|
||||||
|
[1],
|
||||||
|
[2],
|
||||||
|
[1, 2],
|
||||||
|
[3],
|
||||||
|
[1, 3],
|
||||||
|
[2, 3],
|
||||||
|
[1, 2, 3]
|
||||||
|
])
|
||||||
|
expect(generateSubSets([1, 2])).toEqual([[], [1], [2], [1, 2]])
|
||||||
|
expect(generateSubSets([1, 2, 3])).toEqual([
|
||||||
|
[],
|
||||||
|
[1],
|
||||||
|
[2],
|
||||||
|
[1, 2],
|
||||||
|
[3],
|
||||||
|
[1, 3],
|
||||||
|
[2, 3],
|
||||||
|
[1, 2, 3]
|
||||||
|
])
|
||||||
|
expect(() => generateSubSets('invalid')).toThrow(
|
||||||
|
'Provided input is not an array'
|
||||||
|
)
|
||||||
|
expect(() =>
|
||||||
|
generateSubSets([
|
||||||
|
1, 2, 2, 1, 2, 3, 4, 3, 2, 3, 4, 3, 2, 2, 2, 3, 12, 11, 4, 2, 2, 2, 2,
|
||||||
|
1, 2, 3, 5, 6, 7, 7, 8, 6, 5, 6, 7, 8, 9, 8, 0, 6
|
||||||
|
])
|
||||||
|
).toThrow('Error size should be less than equal to 32')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user