Why is my Answer wrong??

All submissions for this problem are available.

For help on this problem, please check out our tutorial Input and Output (I/O)
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely… rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
Example

Input:

1
2
88
42
99

Output:

1
2
88

import java.io.*;
class codechef1
{
public static void main(String args[]) throws IOException
{
	int a[]=new int[10];
	DataInputStream dis=new DataInputStream(System.in);
	for(int j=0;j<5;j++)
	{
	if(a[j]<100)
	{
	 a[j]=Integer.parseInt(dis.readLine());
	}
	}
	
	for(int j=0;j<5;j++)
	{
	if(a[j]!=42)
	{
		System.out.println(a[j]);
	}
	else
	{
		break;
	}
	}
}
 }

Your loop : for(int j=0;j<5;j++) runs only for 5 times. The number of input numbers is not 5 for all cases. You will need to continue taking input until the number 42 comes. The number 42 might come after 100-200 numbers. However your code runs only for 5 elements. Just extend it to continue taking input till 42 comes and it will get accepted.

You are getting the wrong answer because your solution doesn’t accommodate all possible inputs. It only works for those that are similar to the example case given in the problem. You need to continue reading and printing integers until you reach 42 while that condition hasn’t been met you must proceed. Your code is set to do this for 5 inputs but there is no indication that there can’t be more than 5.

You beat me to it :slight_smile:

Link to the problem for convenience: http://www.codechef.com/problems/TEST

Sample test cases doesn’t imply that the number of test cases will be equal to 5. As name shows, its just a sample case. So at runtime you may come across a larger input. Max. number of test cases is not known. So try for an array with max. possible size. Or printing out numbers(if satisfying condition) as soon as input is taken could also be done.

Sorry man, Had no idea you were also answering.

//