Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
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 Oct 26, 2023
3570034
Updated Documentation in README.md
Oct 26, 2023
786954b
Added Tug of war problem link
vedas-dixit Oct 26, 2023
e9f0b58
Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit Oct 26, 2023
d549ac9
Merge branch 'TheAlgorithms:master' into tug-of-war-backtracking
vedas-dixit Oct 26, 2023
dcf5c2c
Updated Documentation in README.md
Oct 26, 2023
6c88665
Merge branch 'master' of https://github.com/vedas-dixit/JavaScript in…
vedas-dixit Oct 28, 2023
67d1512
Merge branch 'TheAlgorithms:master' into tug-of-war-backtracking
vedas-dixit Oct 30, 2023
7c7c78e
Updated Documentation in README.md
Oct 30, 2023
5c84556
Merge branch 'master' of https://github.com/vedas-dixit/JavaScript in…
vedas-dixit Oct 30, 2023
14ec68f
Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit Oct 30, 2023
93c54dd
Refactor tugOfWar: remove unused vars, optimize initialization, and r…
vedas-dixit Oct 30, 2023
08059d4
Added Function Export Statment
vedas-dixit Oct 30, 2023
2700149
Resolved Changes
vedas-dixit Oct 30, 2023
a48be6c
Updated Documentation in README.md
Oct 30, 2023
964239d
Resolved Code Style --Prettier
vedas-dixit Oct 30, 2023
16bc814
Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit Oct 30, 2023
d597154
Rename "backtrack" to "recurse"
appgurueu Nov 5, 2023
cd460e9
Fix test case: The difference needs to be exactly 1.
appgurueu Nov 5, 2023
55307a0
Code Modification: subsets should have sizes as close to n/2 as possible
vedas-dixit Nov 5, 2023
9cfd3f2
Updated test-case of TugOfWar
vedas-dixit Nov 5, 2023
f7c430a
Changed TugOfWar problem to Partition
vedas-dixit Nov 5, 2023
7f0478c
Modified partition problem
vedas-dixit Nov 5, 2023
67c5f72
Updated Documentation in README.md
Nov 5, 2023
64cf5ae
fixed code style
vedas-dixit Nov 5, 2023
937c545
Merge branch 'tug-of-war-backtracking' of https://github.com/vedas-di…
vedas-dixit Nov 5, 2023
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
* [NumberOfIslands](Graphs/NumberOfIslands.js)
* [PrimMST](Graphs/PrimMST.js)
* **Hashes**
* [MD5](Hashes/MD5.js)
* [SHA1](Hashes/SHA1.js)
* [SHA256](Hashes/SHA256.js)
* **Maths**
Expand Down Expand Up @@ -302,6 +303,7 @@
* [Palindrome](Recursive/Palindrome.js)
* [SubsequenceRecursive](Recursive/SubsequenceRecursive.js)
* [TowerOfHanoi](Recursive/TowerOfHanoi.js)
* [TugOfWar](Recursive/TugOfWar.js)
* **Search**
* [BinarySearch](Search/BinarySearch.js)
* [ExponentialSearch](Search/ExponentialSearch.js)
Expand Down
37 changes: 37 additions & 0 deletions Recursive/TugOfWar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @see https://www.geeksforgeeks.org/tug-of-war/
* Divides the given array 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 totalSum = arr.reduce((a, b) => a + b, 0)
let minimumDiff = totalSum
let result = [arr, []]

function recurse(subset, index) {
if (index === arr.length) {
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.
recurse([...subset, arr[index]], index + 1)

// Exclude the current element.
recurse([...subset], index + 1)
}

recurse([], 0)
return result
}

export { tugOfWar }
12 changes: 12 additions & 0 deletions Recursive/test/TugOfWar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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).toBe(1)
})
})