2 / 2
Jan 2011

Hi
I wrote this code for 8132. Street Trees

Problem code: STREETR

i get wrong answer though i tested it against possible test cases in my mind ..
can some body help please ???? cry

import java.util.Scanner;
public class Main
{
	public static void main (String args[])
	{	
		int noofTree=0;
		int counter=0;
		Scanner scan= new Scanner(System.in);
//		System.out.print("Enter the No of Trees\n");
		try{
		noofTree= scan.nextInt();
		}
		catch(NumberFormatException nfe)
		{
			nfe.printStackTrace();
		}
		int []anArray;
//		System.out.print("Enter the Co-ordinates\n");
		anArray= new int[100];
		if(noofTree>=3)
		{	
		 for (int i=0;i<noofTree;i++)
		 {
			try{
			anArray[i]=scan.nextInt();
			}
			catch(NumberFormatException nfe)
			{
				nfe.printStackTrace();
			}
			System.out.print(anArray[i]);
		}
		}
	    int i=0;
	    int k=1;
	    int diff=0;
	    int max=0;
	    max=anArray[--noofTree];	    
	    diff=anArray[k]-anArray[i];
//	    System.out.print("Diff="+diff);
	    k=1;
loop:	for(int no=diff;no>0;no--)
	    {
	    	int var=anArray[i];
	    	while(var<=max)
	    	{
	    			var+=no;
	    			if(var>max)
	    			{
	    				counter=0;
	    				k=1;
	    				continue loop;
	    			}
	    			if(var!=(anArray[k]))
    				{
    					counter++;
    				}
	    			if (var==anArray[k])
	    			{                
	    				if(var==max)
	    				{
	    					System.out.print("\n"+counter);
	    					System.exit(0);
	    				}		
	    				if(var<=anArray[k])
	    				{
	    					k++;
	    				}
	    			}
	    	}
	    }
	}
}
  • created

    Jan '11
  • last reply

    Jan '11
  • 1

    reply

  • 232

    views

  • 2

    users

By pressing the code button above the text box where you enter your code will display as I have made it appear in your post above.

Please explain your urgency? Nothing around this forum is typically urgent, and if it were, you should be asking someone for help face to face. People around here are typically pretty prompt, but they are mostly volunteers, and will provide solutions as they have time.

You definition of max is based in an unfounded assumption.

You code does not return the proper result for the sample input.

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.