-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathK_equal_sum_subset.java
More file actions
39 lines (39 loc) · 1.56 KB
/
K_equal_sum_subset.java
File metadata and controls
39 lines (39 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class K_equal_sum_subset {
//helper function
boolean helpInPartition(int nums[],boolean visited[],int start,int k,int currentSum,int targetSum)
{
//when there are no more subsets left to make
if(k==0)
return true;
if(currentSum>targetSum)
return false;
//if current sum equals target sum,we are left with k-1 subsets to make
if(currentSum==targetSum)
return helpInPartition(nums,visited,0,k-1,0,targetSum);
for(int j=start;j<nums.length;j++)
{
//if nums[j] is already included in a subset skip this iteration
if(visited[j])
continue;
//let us assume nums[j] is a part of the current subset
visited[j]=true;
//current sum < target sum,we look for elements beyond j
if(helpInPartition(nums,visited,j+1,k,currentSum+nums[j],targetSum))
return true;
//if nums[j] is not a part of the current subset , we mark it unvisited
visited[j]=false;
}
return false;
}
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum=0;
for(int i:nums)
sum+=i;
//nums can be divided into k equal sum subsets only if the sum of its elements is divisible by k
if(sum%k!=0)
return false;
//visited array to keep track where an element is already a part of a subset or not
boolean visited[]=new boolean[nums.length];
return helpInPartition(nums,visited,0,k,0,sum/k);
}
}