Partition Equal Subset Sum Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Partition Equal Subset Sum runs in O(n*S) time and O(S) space, in JavaScript as in every other language.
- ✓There is no bitset in the standard library, and two real substitutes. `new Uint8Array(target + 1)` gives a dense byte-per-cell table with `.fill()` that can never go holey.
- ✓Reviewers ask for `Uint8Array` over `new Array(target + 1).fill(false)`: the typed array cannot go holey and cannot hold a boxed value.
- ✓The JavaScript trap: The Number-based bitset is wrong in a way every small test passes: `1 << 31` is -2147483648 and `1 << 32` is `1`, because both operands are coerced to signed 32-bit and the shift count is taken mod 32.
- ✓A `Uint8Array` keeps one elements-kind for its whole life; `new Array(n)` starts as HOLEY_SMI_ELEMENTS; the first `false` stored transitions it once to HOLEY_ELEMENTS, invalidating code optimised for the old kind.
What Partition Equal Subset Sum actually is
Partition Equal Subset Sum in JavaScript: what you actually reach for
JavaScript implementation
function canPartition(nums) {
let total = 0;
for (const n of nums) total += n;
if (total % 2 !== 0) return false;
const target = total / 2;
// Number bitwise ops are 32-bit. BigInt gives a real arbitrary-width bitset.
const t = BigInt(target);
let bits = 1n;
for (const n of nums) {
if (n > target) return false;
bits |= bits << BigInt(n);
if ((bits >> t) & 1n) return true;
}
return ((bits >> t) & 1n) === 1n;
}
function canPartitionBytes(nums) {
const total = nums.reduce((a, b) => a + b, 0);
if (total % 2 !== 0) return false;
const target = total / 2;
const dp = new Uint8Array(target + 1); // dense, never holey, never boxed
dp[0] = 1;
for (const n of nums) {
if (n > target) return false;
for (let j = target; j >= n; j--) {
if (dp[j - n] === 1) dp[j] = 1;
}
if (dp[target] === 1) return true;
}
return dp[target] === 1;
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n*S)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Partition Equal Subset Sum?
There is no bitset in the standard library, and two real substitutes. `new Uint8Array(target + 1)` gives a dense byte-per-cell table with `.fill()` that can never go holey. Or `BigInt`, which supports `<<`, `|` and `>>` at arbitrary width: `bits |= bits << BigInt(n)`, tested with `(bits >> BigInt(target)) & 1n`. Plain `Number` bitwise operators are not an option - `&`, `|` and `<<` run ToInt32 on both operands first, so a Number bitset silently caps at 32 sums. `Array.prototype.reduce` handles the total; `Set` handles the sparse variant.
What is the most common JavaScript mistake when implementing Partition Equal Subset Sum?
The Number-based bitset is wrong in a way every small test passes: `1 << 31` is -2147483648 and `1 << 32` is `1`, because both operands are coerced to signed 32-bit and the shift count is taken mod 32. Any input whose half-sum exceeds 31 loses bits in silence. The other JavaScript-only trap is the `Set` fold: unlike Python's RuntimeError and Java's ConcurrentModificationException, the spec says a Set iterator visits values appended during iteration, so `for (const s of seen) seen.add(s + n)` does not throw - it spins forever, growing the Set until the tab dies.
How does Partition Equal Subset Sum actually perform in JavaScript?
A `Uint8Array` keeps one elements-kind for its whole life; `new Array(n)` starts as HOLEY_SMI_ELEMENTS; the first `false` stored transitions it once to HOLEY_ELEMENTS, invalidating code optimised for the old kind. The transition is one-way and happens at most a few times, but every later read pays the holey path - a prototype-chain check per miss - which the typed array never pays. Expect the first few thousand iterations to run in the interpreter before the optimising tier engages, so timing a single call measures warmup, not the algorithm. `BigInt` values are heap-allocated and immutable, so each `<<` and `|` allocates a fresh multi-word object - for targets in the low thousands the Uint8Array loop usually wins outright despite doing more instructions. Time complexity is O(n*S) and space is O(S).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialMost AI and dev tools have a student plan. See which ones and how to claim them on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.