How to take Input of starting and ending point

I am not able to understand how I take input of starting and ending time in form of 2D array of size nX2 or pairs or something else.

hi @jinisha25jain565503

Approach

A basic activity selection problem which uses Greedy approach. The implementation mentioned here states to sort the activities in the increasing order of their finishing times. Initially, we choose the first activity. Now, starting from second activity, current activity can be selected if the finish time of previous selected activity is less than or equal to the starting time of current activity.

Code

#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b) {
    return a.second < b.second;
}

int main(int argc, char const *argv[])
{
    int t;
    cin>>t;
    while(t--) {
        int n, a, b;
        cin>>n;
        pair<int, int> arr[n];
        for(int i=0;i<n;i++) {
            cin>>a>>b;
            arr[i] = make_pair(a, b);
        }
        sort(arr, arr+n, cmp);
        int ans = 1, chosen = 0;
        for(int i=1;i<n;i++) {
            if(arr[i].first>=arr[chosen].second) {
                ans++;
                chosen = i;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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.