Partition to K Equal Sum Subsets

MediumBacktrackingbitmask dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums and an integer k, decide whether the elements can be split into exactly k non-empty groups with equal sums. Every element must belong to exactly one group. Return true if such a split exists and false otherwise.

Examples

Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Groups (5), (1,4), (2,3), (2,3) each sum to 5.
Input: nums = [1,2,3,4], k = 3
Output: false
The total 10 is not divisible by 3.

Constraints

  • 1 <= k <= nums.length <= 16
  • 1 <= nums[i] <= 10^4

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bitmask dp problems