Test Case 0 not passing

My first test case is passing but the 0th test case is not passing for some reason. Thanks in advance.
link: https://ide.codingblocks.com/s/367456

hello @Divye-Aggarwal-3050062625037589

u forgot to sort ur array.
this is the complete logic.
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.

if interested in implementation then check this->

#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);
        // for(int i=0;i<n;i++) {
        //  cout<<arr[i].first<<" "<<arr[i].second<<endl;
        // }
        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;
}
1 Like

can’t we solve this problem using 2d array?

U need to Sort ur 2d matrix and then u can apply the same logic on it

1 Like

So we’ll have to sort the 2d array column wise right ? The one with the finish time ?

Yeah right . . . . . . .

1 Like

okay thanks a lot !!