Confusion about my two coded answers

For this problem, I wrote a solution and which is this:

#include
#include
using namespace std;
int main () {
int i = 1;
int n;
cin >> n;
int largest = INT_MIN;
int no;
while (i <= n) {
cin >> no;
if(largest < no){

        largest = no;
    }
    i = i+1;
}
cout << "largest number : " << largest;


cout << endl;
return 0;

}

N.B: In my opinion, this should be work if I take more numbers than 3.

but, when I submitted this solution, I found out this is not the answer.

Then I again did some code with another technique and which is this:

#include
using namespace std;

int main() {
int a, b, c;

cin >> a >> b >> c;

int largest;
if (a >= b && a >= c)
    largest = a;
else if (b >= a && b >= c)
    largest = b;
else
    largest = c;

// Output the result
cout << largest << endl;

return 0;

}

but, why this is the optimal answer and why the previous one is not the answer. please explain