-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivitySelectionProblem.c
More file actions
53 lines (48 loc) · 1.6 KB
/
ActivitySelectionProblem.c
File metadata and controls
53 lines (48 loc) · 1.6 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
void findMaxActivities(int n,int arr1[n],int depr[n]);
int main(){
printf("Give number of activities: ");
int n;
scanf("%d",&n);
int arrl[n],depr[n];
for(int i = 0; i<n; i++){
printf("%d activity\n",i+1);
printf("Start time of %d activity: ",i+1);
scanf("%d",&arrl[i]);
printf("Finish time of %d activity: ",i+1);
scanf("%d",&depr[i]);
}
findMaxActivities(n,arrl,depr);
return 0;
}
int compareIntegers(void *x1,void *x2){
return *(int *) x1 - *(int *) x2;
}
void findMaxActivities(int n,int arrl[n],int depr[n]){
qsort((void *) arrl, n, sizeof(int), compareIntegers); //sort the tables
qsort((void *) depr, n, sizeof(int), compareIntegers);
int depr_time[n];
int activitiesIn = 1,maxActivities = 1;
depr_time[0] = depr[0];
int i = 1, j = 0,k = 0;
while (i < n && j < n){
if (arrl[i] < depr[j]){
activitiesIn++;
if (activitiesIn >= maxActivities){
if(activitiesIn == maxActivities)k++; //k is to keep the number of the same activities in 2 or more time periods
else k = 0;
maxActivities = activitiesIn;
depr_time[k] = depr[j]; //departure time of max activity
}
i++;
}
else{
activitiesIn--;
j++;
}
}
for(i=0; i<=k; i++){
printf("Maximum Number of Activities : %d at time period %d-%d\n",maxActivities,depr_time[i]-1,depr_time[i]);
}
}