# Activity Selection Problem in C++

Activity Selection is a classical problem in computer science, which can be solved using greedy algorithms. In this problem, we are given a set of activities that are to be performed during a given period, and each activity has a start time and an end time. The goal is to select a maximum number of activities that can be performed, such that no two activities overlap.

Let's consider an example to understand this problem better. Suppose you have five activities: A, B, C, D, and E, and the start and end times of each activity are as follows:

A: start = 1, end = 4

B: start = 3, end = 5

C: start = 0, end = 6

D: start = 5, end = 7

E: start = 8, end = 9

We can see that if we perform activity A and D, there will be a conflict, as both activities overlap. So, the goal is to select a maximum number of activities that can be performed without any conflict. The solution to this problem can be found using a greedy algorithm. The idea behind the greedy algorithm is to always select the next activity that finishes first. This ensures that we have maximum time to complete the next activity.

Here's how we can implement the activity selection problem in C++:

C++ Code

In the above code, we first sort the activities based on their end time, as we want to select the activity that finishes first. Then, we use two pointers, i and j, to keep track of the current activity and the next activity. We start with i = 0 and j = 1, and compare the start time of the next activity with the end time of the current activity. If the start time of the next activity is greater than or equal to the end time of the current activity, we can perform both activities without any conflict, so we increment j and set i = j. This continues until j reaches the end of the array. The time complexity of this algorithm is O(nlogn), as we need to sort the activities first, which takes O(nlogn) time. The space complexity of this algorithm is O(1), as we are not using any extra space.

Output

```Following activities are selected:
(1, 2), (3, 4), (5, 7), (8, 9),
```

Explanation:

The above code implements the activity selection problem using a greedy algorithm in C++. The activity selection problem is a classical problem in computer science where we are given a set of activities to be performed during a given period, and each activity has a start time and an end time. The goal is to select a maximum number of activities that can be performed without any conflict, such that no two activities overlap.

The code starts with the inclusion of the bits/stdc++.h header file, which includes all the standard C++ libraries. Then, the Activity structure is defined to store the start and end times of each activity. Next, the compare function is defined, which is used to sort the activities based on their end time. The reason we sort the activities based on their end time is because we want to select the activity that finishes first. This ensures that we have maximum time to complete the next activity.

The printMaxActivities function takes an array of activities and its size as arguments, and implements the greedy algorithm to solve the activity selection problem. The function starts by sorting the activities based on their end time using the sort function and the compare function.

### Feedback   