-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: added find subsets algorithm using bitmanipulation #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raklaptudirm
merged 6 commits into
TheAlgorithms:master
from
Nikunj-bisht:generate-subsets
Oct 26, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf9ad50
feat: added find subsets algorithm using bitmanipulation
Nikunj-bisht aeca9a8
file name fix
Nikunj-bisht 3ae0a36
test file name fix
Nikunj-bisht cdf1ca2
fix: codespell fix
Nikunj-bisht cac6f14
error handled
Nikunj-bisht 36dbdfe
added test cases for error
Nikunj-bisht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.