Please help me with this question

the link is here to the question : https://www.codechef.com/problems/LOSTMAX

my solution:

#include
using namespace std;
int main()
{
int T,N,max;
cin>>T;
cin>>N;
int A[++N];
max=A[0];
while(T)
{

	for(int i=0;i<N-1;i++)
	{
		cin>>A[i];
		if(A[i]==N){}
		else if(A[i]>max)
			max=A[i];
    }
	T--;
}
cout<<endl<<max;
return 0;

}

I think you are not getting input right due to unknown number of values .You can use string with cin.peek() or stringstream for input then convert value into integer.

The error is only in your assignment for the max. You initialized it with A[0] which is (by then) just an empty array. Since there’s no value inside, the value of A[0] is randomized within the limits of what an int can hold (your first max’s value is EXTRA LARGE).

You can rest easy by just switching A[0] with plain old 0, such as:


max = 0;

Hope it helps.

Tip: since ‘max’ is a reserved variable, you may try switching it with ‘mx’ or any other instead to reduce confusion. :slight_smile:

It should if A[i]==N-1 as n is one the numbers so len in one more than it should be.

@tpriyanshu90 the error is in “cin>>N” statement. In ur case u are assuming that first value is N but it’s clearly mentioned in the question that first value may and may not be N. :slight_smile:
Also i didn’t checked rest of ur logic.

@hitik: Because of the line:


int A[++N];

The original value of N became appended to N+1 so the succeeding statements are correct. But I forgot there’s no N given after T, only the N+1 long line for each case…

@tpriyanshu90: Didn’t notice that. The input should be T at first line and then every succeeding lines will contain N+1 integers with N mixed within at random.