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
|
n,m,k =>3,3,2
Array:[1,2,3]
Output: 1 Because there is only 1 subset of size k divsible by m {1,2}
My function looks like:
void combinationUtil(int arr[], int n, int k, int index,int data[], int i,int m)
{
if (index == k)
{
int sum=0;
for (int j = 0; j < k; j++)
sum=sum+data[j];
if(sum%m==0)
{
cnt=(cnt%MOD+1)%MOD;
}
return;
}
if (i >= n)
return;
data[index] = arr[i];
combinationUtil(arr, n, k, index + 1, data, i + 1,m);
combinationUtil(arr, n, k, index, data, i + 1,m);
}
|