Test Case not working while the answer is right

This is the program that i first implemented without arrays:

#include
#include
using namespace std;

int main()
{
int t, i, x, y, N, j;
cin >> t;
for (i = 0; i < t; i++)
{
int maximum = 0, activityCount = 0;
cin >> N;
cin >> x >> y;
maximum = max(x, y);
activityCount++;
for (j = 1; j < N; j++)
{
cin >> x >> y;
if (maximum <= min(x, y))
{
activityCount++;
maximum = max(x, y);
}
}
cout << activityCount;
}

return 0;

}

This is the program with arrays :

#include
#include
using namespace std;

int main()
{
int t, i, arr[20000], N, j;
cin >> t;
for (i = 0; i < t; i++)
{
int maximum = 0, activityCount = 0;
cin >> N;
cin >> arr[0] >> arr[1];
maximum = max(arr[0], arr[1]);
activityCount++;
for (j = 2; j < N*2; j += 2)
{
cin >> arr[i] >> arr[i + 1];
if (maximum <= min(arr[i], arr[i + 1]))
{
activityCount++;
maximum = max(arr[i], arr[i + 1]);
}
}
cout << activityCount;
}

return 0;

}

Even though both program work , why is the test case failing . I think you want me to use structure/vectors but can’t i just solve this simply with arrays.

No we don’t want that
you can solve question in any way
but output should be correct for all testcase

Your approach is not correct

for this question first you have to sort the array and only then preform operation on it

you can use this simple check
if(arr[i]>=max){
cnt++;
max=arr[i+1];

}

as arr[i] will be always smaller than arr[i+1] because
arr[i] is starting time and arr[i+1] is ending time

#include

#include

using namespace std;

int main()

{

int t, i, arr[10000], N, j;

cin >> t;

for (i = 0; i < t; i++)

{

    int maximum = 0, activityCount = 0;

    cin >> N;

    for (j = 0; j < N * 2; j++)

    {

        cin >> arr[j];

    }

    maximum = 0;

    for (j = 0; j < N*2; j += 2)

    {

        if (arr[j] >= maximum)

        {

            activityCount++;

            maximum = arr[j + 1];

        }

    }

    cout << activityCount;

}

return 0;

}

I have used your logic and the program is giving right output BUT the test cases are not being corrected.

for this approach to work you have to sort the array according to start time