Can anyone help me to sort out the correct code for the following:

import java.io.*;
class life
{
int i,x;
System.out.println(“Input:”);
int a[]=new int[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<100;i++)
{
a[i]=Integer.parseInt(br.readLine());
if(a[i]==42)
{
x=i;
break;
}
System.out.println();
}
System.out.println(“Output:”);
for(i=0;i<x;i++)
{
println(a[i]);
}
}

@ishaniit competitive programming doesn’t require these types of lines:

  1. System.out.println(“Input:”);

  2. System.out.println(“Output:”); etc. unless mentioned in the problem.

Now about your solution, you don’t need to run this loop → for(i=0;i<100;i++) as the question says stop procesing when you encounter number 42 so the question contains infinte input and output all the numbers before 42.

You can read the editorial here.