Activity Selection Problems (explanation and soln in cpp)

You are given n activities (from 0 to n-1) with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Input Format

The first line consists of an integer T, the number of test cases. For each test case, the first line consists of an integer N, the number of activities. Then the next N lines contain two integers m and n, the start and end time of each activity.
Constraints

1<=t<=50
1<=n<=10000
1<=A[i]<=100
Output Format

For each test case find the maximum number of activities that you can do.
Sample Input

1
3
10 20
12 15
20 30

Sample Output

2

Explanation

Person can only do 0th and 2nd activities.

You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.

Example:

    Consider the following 6 activities. 
         start[]  =  {1, 3, 0, 5, 8, 5};
         finish[] =  {2, 4, 6, 7, 9, 9};
    The maximum set of activities that can be executed 
    by a single person is {0, 1, 3, 4}

The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity.

  1. Sort the activities according to their finishing time
  2. Select the first activity from the sorted array and print it.
  3. Do following for remaining activities in the sorted array.
    …….a) If the start time of this activity is greater than the finish time of previously selected activity then select this activity and print it

first you should try and then i will provide you the solution

ok I TRIED NOW UR SOLN PLS

Reference Code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.