0
0
DSA Cprogramming~10 mins

Activity Selection Problem in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the array that stores the finish times of activities.

DSA C
int finish[] = {1, 3, 6, 5, 8, 9};
int start[] = {0, 1, 3, 4, 6, 8};
int n = sizeof(finish)/sizeof(finish[0]);

// The array to store finish times is called [1];
Drag options to blanks, or click blank then click option'
Astart
Bfinish
Cactivities
Dtimes
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing start times with finish times.
Using an undefined variable name.
2fill in blank
medium

Complete the code to select the first activity by initializing the index.

DSA C
int i = 0; // Select the first activity
int count = 1; // Count of selected activities

for (int j = 1; j < n; j++) {
    if (start[j] >= finish[[1]]) {
        i = j;
        count++;
    }
}
Drag options to blanks, or click blank then click option'
An
Bcount
Cj
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' instead of 'i' to check finish time.
Comparing with 'count' or 'n' which are not indices.
3fill in blank
hard

Fix the error in the loop condition to correctly iterate over all activities.

DSA C
for (int j = 1; j [1] n; j++) {
    if (start[j] >= finish[i]) {
        i = j;
        count++;
    }
}
Drag options to blanks, or click blank then click option'
A>
B<=
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds access.
Using '>' or '>=' causes the loop to never run.
4fill in blank
hard

Fill both blanks to complete the function header and return statement for counting selected activities.

DSA C
int activitySelection(int [1][], int [2][], int n) {
    int count = 1;
    int i = 0;
    for (int j = 1; j < n; j++) {
        if ([1][j] >= [2][i]) {
            i = j;
            count++;
        }
    }
    return count;
}
Drag options to blanks, or click blank then click option'
Astart
Bfinish
Cactivities
Dtimes
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping start and finish arrays.
Using incorrect parameter names.
5fill in blank
hard

Fill all three blanks to complete the main function that calls activitySelection and prints the result.

DSA C
#include <stdio.h>

int activitySelection(int [1][], int [2][], int n);

int main() {
    int start[] = {1, 3, 0, 5, 8, 5};
    int finish[] = {2, 4, 6, 7, 9, 9};
    int n = sizeof(start)/sizeof(start[0]);

    int result = activitySelection(start, finish, [3]);
    printf("Number of activities selected: %d\n", result);
    return 0;
}
Drag options to blanks, or click blank then click option'
Astart
Bfinish
Cn
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Passing arrays in wrong order.
Passing wrong variable for number of activities.