Consider the 2-member k-group formation problem in a class of size n, where the number of
groups, 𝑘 ≤n/2
, is a user input. We also want the members of a group to be close to each
other in terms of their dates of birth. In other words, the sum total of difference between
dates of birth of two members in a group, over all the groups, should be low .
. A simple strategy is to compute the age difference between all pairs of
students, and compute top k disjoint pairs. Implement this algorithm. What is the complexity
of this algorithm?
What I have tried:
#include <stdio.h>
#include<math.h>
int count;
int k=0;
void solve(int arr[], int n, int r, int index, int data[], int i,int a2[]){
int j;
if (index == r) {
for ( j = 0; j < r; j++){
printf("%d ", data[j]);
a2[k]=data[j];
k++;
}
printf("\n");
count++;
return;
}
if (i >= n)
return;
data[index] = arr[i];
solve(arr, n, r, index + 1, data, i + 1,a2);
solve(arr, n, r, index, data, i + 1,a2);
}
int main()
{
int n,k,i,j=0;
printf("Enter value of n: ");
scanf("%d",&n);
int arr[n],a2[12],age[6];
printf("Enter values:\n");
for( i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter value of k: ");
scanf("%d",&k);
int data[k];
count=0;
solve(arr, n, k, 0, data, 0,a2);
printf("Number of Subsets printed: %d",count);
for ( i = 0; i < 12-1; i+=2)
{
age[j]=abs(a2[i]-a2[i+1]);
j++;
}
for ( i = 0; i < j; i++)
{
printf("%d\n",age[i]);
}
return 0;
}