forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from TheAlgorithms:master #76
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
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
Implemented Partition Problem, Recursive problem (TheAlgorithms#1582)
* Add Tug of War solution using backtracking * Updated Documentation in README.md * Added Tug of war problem link * Updated Documentation in README.md * Updated Documentation in README.md * Refactor tugOfWar: remove unused vars, optimize initialization, and remove redundant checks * Added Function Export Statment * Updated Documentation in README.md * Resolved Code Style --Prettier * Rename "backtrack" to "recurse" * Fix test case: The difference needs to be exactly 1. * Code Modification: subsets should have sizes as close to n/2 as possible * Updated test-case of TugOfWar * Changed TugOfWar problem to Partition * Modified partition problem * Updated Documentation in README.md * fixed code style --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
- Loading branch information
commit e9e3ea4684332263fa7afbdd0e18780158888e7d
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
39 changes: 39 additions & 0 deletions
Recursive/Partition.js
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,39 @@ | ||
/** | ||
* @function canPartition | ||
* @description Check whether it is possible to partition the given array into two equal sum subsets using recursion. | ||
* @param {number[]} nums - The input array of numbers. | ||
* @param {number} index - The current index in the array being considered. | ||
* @param {number} target - The target sum for each subset. | ||
* @return {boolean}. | ||
* @see [Partition Problem](https://en.wikipedia.org/wiki/Partition_problem) | ||
*/ | ||
|
||
const canPartition = (nums, index = 0, target = 0) => { | ||
if (!Array.isArray(nums)) { | ||
throw new TypeError('Invalid Input') | ||
} | ||
|
||
const sum = nums.reduce((acc, num) => acc + num, 0) | ||
|
||
if (sum % 2 !== 0) { | ||
return false | ||
} | ||
|
||
if (target === sum / 2) { | ||
return true | ||
} | ||
|
||
if (index >= nums.length || target > sum / 2) { | ||
return false | ||
} | ||
|
||
// Include the current number in the first subset and check if a solution is possible. | ||
const withCurrent = canPartition(nums, index + 1, target + nums[index]) | ||
|
||
// Exclude the current number from the first subset and check if a solution is possible. | ||
const withoutCurrent = canPartition(nums, index + 1, target) | ||
|
||
return withCurrent || withoutCurrent | ||
} | ||
|
||
export { canPartition } |
24 changes: 24 additions & 0 deletions
Recursive/test/Partition.test.js
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,24 @@ | ||
import { canPartition } from '../Partition' | ||
|
||
describe('Partition (Recursive)', () => { | ||
it('expects to return true for an array that can be partitioned', () => { | ||
const result = canPartition([1, 5, 11, 5]) | ||
expect(result).toBe(true) | ||
}) | ||
|
||
it('expects to return false for an array that cannot be partitioned', () => { | ||
const result = canPartition([1, 2, 3, 5]) | ||
expect(result).toBe(false) | ||
}) | ||
|
||
it('expects to return true for an empty array (0 elements)', () => { | ||
const result = canPartition([]) | ||
expect(result).toBe(true) | ||
}) | ||
|
||
it('Throw Error for Invalid Input', () => { | ||
expect(() => canPartition(123)).toThrow('Invalid Input') | ||
expect(() => canPartition(null)).toThrow('Invalid Input') | ||
expect(() => canPartition(undefined)).toThrow('Invalid Input') | ||
}) | ||
}) |
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.