Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -300,6 +301,7 @@
* [KochSnowflake](Recursive/KochSnowflake.js)
* [LetterCombination](Recursive/LetterCombination.js)
* [Palindrome](Recursive/Palindrome.js)
* [Partition](Recursive/Partition.js)
* [SubsequenceRecursive](Recursive/SubsequenceRecursive.js)
* [TowerOfHanoi](Recursive/TowerOfHanoi.js)
* **Search**
Expand Down
39 changes: 39 additions & 0 deletions Recursive/Partition.js
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
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')
})
})