Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
Example 1:
Input: nums = [1,5,11,5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 100Problem summary: Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,5,11,5]
[1,2,3,5]
partition-to-k-equal-sum-subsets)minimize-the-difference-between-target-and-chosen-elements)maximum-number-of-ways-to-partition-an-array)partition-array-into-two-arrays-to-minimize-sum-difference)find-subarrays-with-equal-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #416: Partition Equal Subset Sum
class Solution {
public boolean canPartition(int[] nums) {
// int s = Arrays.stream(nums).sum();
int s = 0;
for (int x : nums) {
s += x;
}
if (s % 2 == 1) {
return false;
}
int n = nums.length;
int m = s >> 1;
boolean[][] f = new boolean[n + 1][m + 1];
f[0][0] = true;
for (int i = 1; i <= n; ++i) {
int x = nums[i - 1];
for (int j = 0; j <= m; ++j) {
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
}
}
return f[n][m];
}
}
// Accepted solution for LeetCode #416: Partition Equal Subset Sum
func canPartition(nums []int) bool {
s := 0
for _, x := range nums {
s += x
}
if s%2 == 1 {
return false
}
n, m := len(nums), s>>1
f := make([][]bool, n+1)
for i := range f {
f[i] = make([]bool, m+1)
}
f[0][0] = true
for i := 1; i <= n; i++ {
x := nums[i-1]
for j := 0; j <= m; j++ {
f[i][j] = f[i-1][j] || (j >= x && f[i-1][j-x])
}
}
return f[n][m]
}
# Accepted solution for LeetCode #416: Partition Equal Subset Sum
class Solution:
def canPartition(self, nums: List[int]) -> bool:
m, mod = divmod(sum(nums), 2)
if mod:
return False
n = len(nums)
f = [[False] * (m + 1) for _ in range(n + 1)]
f[0][0] = True
for i, x in enumerate(nums, 1):
for j in range(m + 1):
f[i][j] = f[i - 1][j] or (j >= x and f[i - 1][j - x])
return f[n][m]
// Accepted solution for LeetCode #416: Partition Equal Subset Sum
impl Solution {
pub fn can_partition(nums: Vec<i32>) -> bool {
let s: i32 = nums.iter().sum();
if s % 2 != 0 {
return false;
}
let m = (s / 2) as usize;
let n = nums.len();
let mut f = vec![vec![false; m + 1]; n + 1];
f[0][0] = true;
for i in 1..=n {
let x = nums[i - 1] as usize;
for j in 0..=m {
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
}
}
f[n][m]
}
}
// Accepted solution for LeetCode #416: Partition Equal Subset Sum
function canPartition(nums: number[]): boolean {
const s = nums.reduce((a, b) => a + b, 0);
if (s % 2 === 1) {
return false;
}
const n = nums.length;
const m = s >> 1;
const f: boolean[][] = Array.from({ length: n + 1 }, () => Array(m + 1).fill(false));
f[0][0] = true;
for (let i = 1; i <= n; ++i) {
const x = nums[i - 1];
for (let j = 0; j <= m; ++j) {
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
}
}
return f[n][m];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.