Help with this code?(Still Wrong Answer) LOSTMAX (https://www.codechef.com/problems/LOSTMAX)

I have been trying to solve the new problem under Beginner section LOSTMAX. I ran my code against the input given in the problem’s page but it was showing error, can somebody point out what is wrong in the code?

Here is my solution(Updated from String ArrayList to Integer ArrayList)

    import java.util.*;

class LostMax {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		while (n-- > 0) {
			List<Integer> number = new ArrayList<Integer>();
			String num = in.next();
			num += in.nextLine();
			String[] val = num.split(" ");
			for (int i = 0; i < val.length; i++)
				number.add(Integer.valueOf(val[i]));
			int len = number.size() - 1;
			Iterator<Integer> iter = number.iterator();
			while (iter.hasNext()) {
				if (iter.next().equals(len))
					iter.remove();
			}
			System.out.println(Collections.max(number));

		}
		in.close();
	}
}

It’s giving correct output for the given testcases.
What error did you get?

You are getting WA because Collections.max(number) will return lexicographically biggest string
try running this input for your program: 1 5 1 4 3 100
answer would be 4 because “4”>“100” lexicographically .
your code would work correctly only for single digit input.
You should create a list of Integers and enter the string numbers into it using Integer.valueOf() .
Happy coding!

#include
using namespace std;
int main()
{
int N,T;
int c[100][100],stack[100][100],j,i,k,l;

cin>>T;
for(i=0;i<T;i++)
{
cin>>N;
for(j=0;j<N;j++)
 
{
cin>>c[i][j];
}
 
j=0;
 
while((c[i][j]!=7 && j<N))
{
stack[i][j]=c[i][j];
j++;
 
}
 
k=(j-1);
if(((j+1)*2)!=(N+1))
goto x;
for( l=j+1;l<N;l++)
{
 
if(stack[i][k]==c[i][l])
k--;//ok
 
else
{x:cout<<"no\n";
  
goto l;
}
}
cout<<"yes\n";
l:;
     
}
return 0;
}

This code gives RIGHT answer in ide etc. i.e. yes when it's yes no when no but still not getting it through please somebody help me.

I got this when I ran the code,

https://www.codechef.com/viewsolution/14728287

I changed the code and used Integer ArrayList, but still it is showing wrong answer…
Here is the updated code.

import java.util.*;

class LostMax {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		while (n-- > 0) {
			List<Integer> number = new ArrayList<Integer>();
			String num = in.next();
			num += in.nextLine();
			String[] val = num.split(" ");
			for (int i = 0; i < val.length; i++)
				number.add(Integer.valueOf(val[i]));
			int len = number.size() - 1;
			Iterator<Integer> iter = number.iterator();
			while (iter.hasNext()) {
				if (iter.next().equals(len))
					iter.remove();
			}
			System.out.println(Collections.max(number));

		}
		in.close();
	}
}

@shubham0812 In the While loop :
while(iter.hasNext()){
if(iter.next().equals(len))
iter.remove();
}

You need to add “break” in the if statement. Else it will remove the highest number if the highest number and total input are equal .

alright man that did it, thanks a lot! :slight_smile: