Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

698. Partition to K Equal Sum Subsets

Medium38.1% Acceptance
ArrayDynamic ProgrammingBacktracking
Asked by:
LinkedIn
ProblemHints (1)Solutions (9)VideosCompanies (3)Notes

Problem Statement

Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.

Example 1:

Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.

Example 2:

Input: nums = [1,2,3,4], k = 3
Output: false

Constraints:

  • 1 <= k <= nums.length <= 16
  • 1 <= nums[i] <= 104
  • The frequency of each element is in the range [1, 4].
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
L
G
Google
A
Amazon

Approach

The key idea in #698 Partition to K Equal Sum Subsets is to determine whether an array can be divided into k subsets where each subset has the same total sum. First, compute the total sum of the array and check if it is divisible by k. The target sum for every subset becomes totalSum / k.

A common strategy is backtracking with pruning. We try to place each element into one of the k buckets while keeping track of the current subset sum. Sorting the array in descending order and skipping redundant states helps reduce the search space significantly.

An optimized alternative uses bitmask dynamic programming with memoization. Each bitmask represents which elements are already used, and the DP state stores whether the remaining elements can complete valid subsets. Memoization avoids recomputing the same configurations.

While the search space is exponential, pruning and caching make the solution practical for typical constraints. This problem tests understanding of state representation, recursion, and subset partitioning.

Complexity

ApproachTime ComplexitySpace Complexity
Backtracking with pruningO(k * 2^n) in worst case due to exploring subset combinationsO(n + k) recursion and bucket storage
Bitmask DP with memoizationO(n * 2^n)O(2^n) for memoization states

Video Solution Available

take U forward

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

We can figure out what target each subset must sum to. Then, let's recursively search, where at each call to our function, we choose which of k subsets the next value will join.

Ready to see the solutions?View Solutions

Solutions (9)

Backtracking with Target Subset Sum

This approach involves using backtracking to attempt to build k subsets, each with a sum equal to totalSum / k. The decision tree can be pruned by sorting the numbers in decreasing order and tracking the sums of the current subset.

Time Complexity: O(kn), where n is the number of elements, due to exploring all partition possibilities.
Space Complexity: O(n + k) due to recursive call stack and subset sums array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdbool.h>
3#include <stdlib.h>
4
5int compare(const void *a, 


Explanation

This solution begins by calculating the total sum of elements and checks early if this is divisible by k. It uses sorting to try larger numbers first, which helps in quicker pruning of impossible branches. The recursive function tries to add each number to the current subset sum and backtracks when necessary.

Dynamic Programming and State Compression

A less intuitive but potentially powerful technique involves using bitmasking to represent subsets and top-down dynamic programming to evaluate the feasibility of partitioning. This uses state compression where each state represents a set of elements already partitioned; 1 means used, 0 means unused.

Time Complexity: O(n * 2n), determined by the states and transitions across the full bitmask.
Space Complexity: O(2n), which is used to store DP states for all subset configurations.

CC++Java
1#include <stdio.h>
2





Video Solutions

Watch expert explanations and walkthroughs

DP 15. Partition Equal Subset Sum | DP on Subsequences

take U forward
9:43262,594 views

Asked By Companies

3 companies
L
LinkedIn
G
Google
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Trapping Rain WaterHard
Jump Game IIMedium
Maximum SubarrayMedium
Jump GameMedium
More similar problems

Related Topics

ArrayDynamic ProgrammingBacktrackingBit ManipulationMemoizationBitmask

Problem Stats

Acceptance Rate38.1%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Partition to K Equal Sum Subsets asked in FAANG interviews?

Yes, this problem or similar subset partitioning problems frequently appear in technical interviews at large tech companies. Interviewers use it to evaluate recursion, backtracking optimization, and understanding of state compression techniques like bitmasking.

What data structure is best for Partition to K Equal Sum Subsets?

Arrays and bitmasks are commonly used. Arrays help track current subset sums during backtracking, while bitmasks efficiently represent which elements have already been used in the partition when applying dynamic programming.

What is the optimal approach for Partition to K Equal Sum Subsets?

A widely used approach is backtracking with pruning, where numbers are assigned to k buckets while ensuring each bucket does not exceed the target sum. A more optimized method uses bitmask dynamic programming with memoization to store visited states and avoid recomputation.

Why is sorting helpful in solving Partition to K Equal Sum Subsets?

Sorting the array in descending order helps place larger elements first, which quickly detects invalid configurations. This reduces the number of recursive calls and significantly improves the efficiency of the backtracking solution.

const
void
*
b
)
{
6
return
*
(
int
*
)
b
-
*
(
int
*
)
a
;
7
}
8
9
bool
canPartition
(
int
*
nums
,
int
numsSize
,
int
k
,
int
*
subsetSums
,
int
targetSubsetSum
,
int
index
)
{
10
if
(
index
==
numsSize
)
return
true
;
11
for
(
int
i
=
0
;
i
<
k
;
i
++
)
{
12
if
(
subsetSums
[
i
]
+
nums
[
index
]
<=
targetSubsetSum
)
{
13
subsetSums
[
i
]
+=
nums
[
index
]
;
14
if
(
canPartition
(
nums
,
numsSize
,
k
,
subsetSums
,
targetSubsetSum
,
index
+
1
)
)
return
true
;
15
subsetSums
[
i
]
-=
nums
[
index
]
;
16
}
17
if
(
subsetSums
[
i
]
==
0
)
break
;
// Avoid trying same state again
18
}
19
return
false
;
20
}
21
22
bool
canPartitionKSubsets
(
int
*
nums
,
int
numsSize
,
int
k
)
{
23
int
totalSum
=
0
;
24
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
totalSum
+=
nums
[
i
]
;
25
if
(
totalSum
%
k
!=
0
)
return
false
;
26
int
targetSubsetSum
=
totalSum
/
k
;
27
qsort
(
nums
,
numsSize
,
sizeof
(
int
)
,
compare
)
;
28
int
*
subsetSums
=
(
int
*
)
calloc
(
k
,
sizeof
(
int
)
)
;
29
bool result
=
canPartition
(
nums
,
numsSize
,
k
,
subsetSums
,
targetSubsetSum
,
0
)
;
30
free
(
subsetSums
)
;
31
return
result
;
32
}
33
34
int
main
(
)
{
35
int
nums
[
]
=
{
4
,
3
,
2
,
3
,
5
,
2
,
1
}
;
36
int
k
=
4
;
37
int
numsSize
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
38
printf
(
"%s\n"
,
canPartitionKSubsets
(
nums
,
numsSize
,
k
)
?
"true"
:
"false"
)
;
39
return
0
;
40
}
#
include
<stdbool.h>
3
#
include
<string.h>
4
5
int
dp
[
1
<<
16
]
,
maskSum
[
1
<<
16
]
;
6
7
bool
canPartitionKSubsets
(
int
*
nums
,
int
numsSize
,
int
k
)
{
8
int
totalSum
=
0
;
9
for
(
int
i
=
0
;
i
<
numsSize
;
++
i
)
totalSum
+=
nums
[
i
]
;
10
if
(
totalSum
%
k
!=
0
)
return
false
;
11
int
targetSubsetSum
=
totalSum
/
k
;
12
13
memset
(
dp
,
-
1
,
sizeof
(
dp
)
)
;
14
int
allMask
=
(
1
<<
numsSize
)
-
1
;
15
dp
[
0
]
=
0
;
16
17
// Traverse all states
18
for
(
int
mask
=
0
;
mask
<=
allMask
;
++
mask
)
{
19
if
(
dp
[
mask
]
==
-
1
)
continue
;
20
for
(
int
j
=
0
;
j
<
numsSize
;
++
j
)
{
21
if
(
!
(
mask
&
(
1
<<
j
)
)
&&
dp
[
mask
]
+
nums
[
j
]
<=
targetSubsetSum
)
{
22
int
newMask
=
mask
|
(
1
<<
j
)
;
23
dp
[
newMask
]
=
(
dp
[
mask
]
+
nums
[
j
]
)
%
targetSubsetSum
;
24
}
25
}
26
}
27
28
return
dp
[
allMask
]
==
0
;
29
}
30
31
int
main
(
)
{
32
int
nums
[
]
=
{
4
,
3
,
2
,
3
,
5
,
2
,
1
}
;
33
int
k
=
4
;
34
int
numsSize
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
35
printf
(
"%s\n"
,
canPartitionKSubsets
(
nums
,
numsSize
,
k
)
?
"true"
:
"false"
)
;
36
return
0
;
37
}

Explanation

This approach encodes subsets using bitmasking where the nth bit represents the nth element in the nums array. Dynamic programming is used to store the remainder when each mask's subset sum is divided by targetSubsetSum. A successful partition is found when the full mask covers all elements with a remainder of 0.