K subsets with equal sum
You are given an array of integers "ARR" of length 'N' and an integer 'K'. Your task is to find whether or not you can divide the array "ARR" into 'K' subsets with equal sum.
A subset of an array "ARR" is another array whose every element is present in array "ARR".
For example:
If ARR = {1, 2, 3, 4}, then array {3,4} is a subset of "ARR" because both 3 and 4 are also elements of "ARR".
For example:
Consider array ARR = {3, 5, 2, 4, 4} and K = 2, i.e. you have to divide "ARR" into 2 subsets with equal sum. The division will be {4, 5} and {2, 3, 4} with sum 9.
Note:
Every element of the array must occupy only one subset.
Input Format:
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first line of each test case contains two space-separated integers ‘N’ representing the size of the array and ‘K’.
The second line contains N space-separated integers that represent elements of the array ARR.
Output Format
For each test case, return true if it is possible to divide "ARR" into 'K' subsets of equal sum, false otherwise.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <=1000
1 <= K <= 20
1 <= ARR[i] <= 1000
Time limit: 1 second
CodingNinjas
author
2y
For each test case, print a single line containing “True” if it is possible to divide the array into ‘K' equal sum subsets, “False” otherwise.
The output of each test case will be printed in a separat...read more
CodingNinjas
author
2y
Recursive
Consider function “SPLITARRAY” that accepts array “ARR” and ‘K’ and do
- Check if K=1, that means you do not have to divide array. And the entire array will be the answer, so we return TRUE.
- Chec...read more
Help your peers!
Add answer anonymously...
Top Nagarro Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app