-
-
Couldn't load subscription status.
- Fork 5.8k
Implemented Partition Problem, Recursive problem #1582
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 26 commits into TheAlgorithms:master from vedas-dixit:tug-of-war-backtracking Nov 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
26 commits Select commit Hold shift + click to select a range
26d9599 Add Tug of War solution using backtracking
vedas-dixit 3570034 Updated Documentation in README.md
786954b Added Tug of war problem link
vedas-dixit e9f0b58 Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit d549ac9 Merge branch 'TheAlgorithms:master' into tug-of-war-backtracking
vedas-dixit dcf5c2c Updated Documentation in README.md
6c88665 Merge branch 'master' of https://github.com/vedas-dixit/JavaScript in…
vedas-dixit 67d1512 Merge branch 'TheAlgorithms:master' into tug-of-war-backtracking
vedas-dixit 7c7c78e Updated Documentation in README.md
5c84556 Merge branch 'master' of https://github.com/vedas-dixit/JavaScript in…
vedas-dixit 14ec68f Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit 93c54dd Refactor tugOfWar: remove unused vars, optimize initialization, and r…
vedas-dixit 08059d4 Added Function Export Statment
vedas-dixit 2700149 Resolved Changes
vedas-dixit a48be6c Updated Documentation in README.md
964239d Resolved Code Style --Prettier
vedas-dixit 16bc814 Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit d597154 Rename "backtrack" to "recurse"
appgurueu cd460e9 Fix test case: The difference needs to be exactly 1.
appgurueu 55307a0 Code Modification: subsets should have sizes as close to n/2 as possible
vedas-dixit 9cfd3f2 Updated test-case of TugOfWar
vedas-dixit f7c430a Changed TugOfWar problem to Partition
vedas-dixit 7f0478c Modified partition problem
vedas-dixit 67c5f72 Updated Documentation in README.md
64cf5ae fixed code style
vedas-dixit 937c545 Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit 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
Add Tug of War solution using backtracking
- Loading branch information
commit 26d9599d0afe9f760d1d72a8c9f8fe343b3e7140
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,49 @@ | ||
| // Given an array of n integers, we need to divide them into two subsets such that | ||
| // the difference between their sums is minimized. | ||
| | ||
| | ||
| /** | ||
| * @param {number[]} arr - The array of integers. | ||
| * @returns {Array.<number[]>} Two subsets with minimized sum difference. | ||
| */ | ||
| | ||
| | ||
| function tugOfWar(arr) { | ||
| const n = arr.length; | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| let totalSum = arr.reduce((a, b) => a + b, 0); | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| let minimumDiff = Number.MAX_VALUE; | ||
| let result = []; | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| function isSafe(index, subset) { | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| return index < n; | ||
| } | ||
| | ||
| function backtrack(subset = [], index = 0) { | ||
| if (index === n) { | ||
| const currentSubsetSum = subset.reduce((a, b) => a + b, 0); | ||
| const otherSubsetSum = totalSum - currentSubsetSum; | ||
| const currentDiff = Math.abs(currentSubsetSum - otherSubsetSum); | ||
| | ||
| if (currentDiff < minimumDiff) { | ||
| minimumDiff = currentDiff; | ||
| result = [subset, arr.filter(x => !subset.includes(x))]; | ||
| } | ||
| return; | ||
| } | ||
| | ||
| // Include the current element. | ||
| if (isSafe(index, subset)) { | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| backtrack([...subset, arr[index]], index + 1); | ||
| } | ||
| | ||
| // Exclude the current element. | ||
| if (isSafe(index, subset)) { | ||
vedas-dixit marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| backtrack([...subset], index + 1); | ||
| } | ||
| } | ||
| | ||
| backtrack(); | ||
| return result; | ||
| } | ||
| | ||
| export { tugOfWar }; | ||
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,11 @@ | ||
| import { tugOfWar } from '../TugOfWar'; | ||
| | ||
| describe('tugOfWar', () => { | ||
| it('should divide array into two subsets with minimized sum difference', () => { | ||
| const arr = [3, 4, 5, -3, 100, 1, 89, 54, 23, 20]; | ||
| const [subset1, subset2] = tugOfWar(arr); | ||
| const diff = Math.abs(subset1.reduce((a, b) => a + b, 0) - subset2.reduce((a, b) => a + b, 0)); | ||
| | ||
| expect(diff).toBeLessThanOrEqual(1); // Here we assume that the difference could be 0 or 1 for this test case. | ||
| }); | ||
| }); |
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.