Partition to K Equal Sum Subsets Problem

Given an array of integers and a positive integer 'K', determine if it is possible to divide the array into 'K' non-empty subsets such that the sum of elements in each subset is equal.

Input:

Number of test cases 'T'
For each test case:
First line: Integer 'N', the size of the array
Second line: 'N' space-separated integers representing the array elements
Third line: Integer 'K', number of subsets

Output:

For each test case, print "True" if the array can be divided into 'K' subsets with equal sum, "False" otherwise.

Example:

Input:
2
5
2 1 4 5 3
3
5
6 5 4 3 2
3
Output:
True
False

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 15
  • 0 <= NUMS[i] <= 10^3
  • 1 <= K <= N

Note:

You are not required to print anything; it has already been taken care of. Implement the function to determine the output.
AnswerBot
4mo

The problem involves dividing an array into K subsets with equal sum.

  • Use backtracking to try all possible combinations of dividing the array into K subsets.

  • Keep track of the sum of elements in each su...read more

Help your peers!
Select
Add answer anonymously...

Top Software Analyst Interview Questions Asked at Goldman Sachs

Q. Describe how to implement two stacks using a single array.
Q. Design a stack that supports push, pop, top, and retrieving the minimum element ...read more
Q. What is the difference between SQL and NoSQL databases?
Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits