Click here to Skip to main content
15,886,745 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
}
Posted
Updated 12-Aug-22 1:58am
Comments
Richard Deeming 11-Aug-22 4:08am    
You seem to have forgotten to ask a question.

And no, "do my homework assignment for me" does NOT count as a "question".

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Try with starting some Learn C tutorial or some video tutorial.

You better start now, because it is some work to do.

some tips:
Use functions and structs with understandable names and install some IDE like Visual Studio. Like you should create some struct for the birthday and so can compute the differences.

Best is to sort the persons in birthday order in an array so the half is a good guess.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900