While using C++ to solve problem CARVANS, the following solutions gave different answers. I don’t know why. I am new to C++, so help me.
Problem Link:
https://www.codechef.com/LRNDSA01/problems/CARVANS
The following codes were tested on sample testcase given in the problem.
1st code:
#include <iostream>
using namespace std;
int main()
{
int t;
std::cin >> t;
int n;
int slowest;
int count;
while (t--)
{
std::cin >> n;
if (n == 1) std::cout << 1 << std::endl;
else
{
int nl[n];
for (int i = 0; i < n; i++) std::cin >> nl[i];
count = 1;
slowest = nl[0];
for (int i = 1; i < n; i++)
{
if (slowest >= nl[i]) { count++; slowest = nl[i]; }
}
std::cout << count << std::endl;
}
}
return 0;
}
2nd code:
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
int slowest;
cin >> n;
int nl[n];
for (int i = 0; i < n; i++) cin >> nl[i];
int count = 1;
slowest = nl[0];
for (int i = 1; i < n; i++)
{
if (slowest >= nl[i]) { count++; slowest = nl[i]; }
}
cout << count << endl;
}
return 0;
}