Sunday 7 July 2013

[Problem Solving #1] Project Team (InMobi Challenge question)

A Professor of Physics gave projects to the students of his class. The students have to form a team of two for doing the project. The professor left the students to decide the teams. The number of students in a class will be even.
Each student has a knowledge level. It tells how much knowledge each student has. The knowledge level of a team is the sum of the knowledge levels of both the students.The students decide to form groups such that the difference between the team with highest knowledge and the one with lowest knowledge is minimum.InputFirst line of the input will contain number of test cases t; In the next t lines the first number is n the number of students in the class followed by n integers denoting the knowledge levels of the n studentsOutputYour output should be a single line containing the lowest possible difference between the team with highest knowledge and the one with lowest knowledge.
Sample Input
2
4 2 6 4 3
6 1 1 1 1 1 1
Sample Output
1
0
Explanation
Input Constraints are

1 <= t <= 100

1 <= n <= 100
1 <= knowledge level <= 10000

Program in C:


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int comp(const void *a,const void *b) {
  4.        int *x = (int *) a;
  5.        int *y = (int *) b;
  6. return *x - *y;
  7. }

  8. int find_team_gk(int student_gk[], int count){
  9.   int start=0, end=count-1;
  10. int team_gk[(start+end+1)/2];
  11. int t;
  12. qsort(student_gk, count, sizeof(int), comp);
  13. for (t=0; start+1<=end;t++) {
  14. team_gk[t] = student_gk[start] + student_gk[end];
  15. start++;
  16. end--;
  17. }
  18. if (team_gk[0] > team_gk[1]) 
  19. return (team_gk[0] - team_gk[1]);
  20. else
  21. return (team_gk[1] - team_gk[0]);
  22. }

  23. int main()
  24. {
  25. int testcases, students;
  26.   int *student_gk, *output;
  27.   int i, s;

  28. printf("number of testcases:");
  29. scanf("%d", &testcases);
  30. output = (int *)malloc(testcases * sizeof(int));

  31.    for (i=0; i< testcases; i++) {
  32. printf("Enter number of students and their gk level in order:"):
  33.      scanf("%d", &students);
  34.      student_gk = (int *)malloc(students * sizeof(int));
  35.      for (s=0; s<students; s++) {
  36.      scanf("%d", &student_gk[s]);
  37.      }
  38.      }
  39.       for (i=0; i<testcases; i++)
  40. output[i] = find_team_gk(student_gk, students);

  41.  for (i=0; i<testcases; i++)
  42.      printf("%d\n", output[i]);
  43. return 0;
  44. }



No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...