InterviewPrepKit

Home / Coding / Algorithm & Data Structure / 1-D Dynamic Programming

Partition Equal Subset Sum

medium Original ↗ 00:00

Problem

Given an array nums of positive integers, decide whether it can be split into two groups whose sums are equal. Return True if such a partition exists, otherwise False. Every element must go into exactly one group.

Examples

  • nums = [1,5,11,5]True[1,5,5] and [11] both sum to 11.
  • nums = [1,2,3,5]False — total is 11, which is odd, so no equal split is possible.
  • nums = [2,2,3,5]False — total 12, but no subset sums to 6.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
  • The total sum is at most 20000.

Think about it first

Hint 1 If the two halves are equal, each must sum to total / 2. So first: if total is odd, immediately return False. Otherwise the question becomes "does some subset sum to target = total / 2?"
Hint 2 That's the classic subset-sum problem, itself a 0/1 knapsack: each element is either included or not. Define reachability over sums from 0 to target.
Hint 3 Keep a boolean set/array reachable[s] = can we hit sum s? Start with {0}. For each number, every currently reachable sum s also makes s + num reachable. Iterate sums downward if you compress to a 1-D array, so each number is used at most once.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug