strange code behavior !

Revision en1, by fares, 2015-09-25 10:42:32

hi guys: I was trying to solve this problem -> http://codeforces.me/contest/580/problem/A here is my code :

import java.util.Scanner;

public class Main {

	public static void main(String []args){
		Scanner sc = new Scanner(System.in);
// putting the number of numbers in length.
    String length1=sc.nextLine();
    int length=Integer.parseInt(length1);
    // making an array of size length ,reading the input and putting it inside the array .
    String array[]=new String[length];
	String format=sc.nextLine();
	array=format.split(" ");
	// making help to put the lengths of the nondecreasing subsequences.
	String help="0";
	// making counter to keep track of the length of the nondecreasing subsequences.
	int counter=1;
	// looping
	for (int i = 0; i < array.length-1; i++) {
		// using parseLong to convert the elements of the array to the  long type (not using parseInt because the elements may be huge )
		
		/* testing if the current element <=the next one and incrementing counter if true , 
		 * else put the counter in help if it is bigger than it's current item and then reseting counter to 1*/
		if (Long.parseLong(array[i])<=Long.parseLong(array[i+1])) {
			++counter;
			// tracing the changes happening to counter and i.
			System.out.println(counter + " "+ i);
		}else{
			
			if (counter>Long.parseLong(help)) {
				System.out.println(counter + " "+ i);

				help=counter+"";
				counter=1;
				//  tracing counter here either.
				System.out.println(counter);
			}
		
		}

	}
	// the last counter value will not be tested and put in help ,so test it and put it in help if it is bigger than it's current element.
	if(counter>Long.parseLong(help))
		help=counter+"";
	// printing the answer.
		System.out.println(help);
	
	

		}
}

now when i test it using this input this occurs: input: 10 100 1000 100 1000 100 1000 100 1000 100 1000 output: 2 0 2 1 1 2 2 // (there should be a 2 3 and a 1 here. this isn't part of the output) 3 4 3 5 1 2 6 3 8 3 // the answer

you will notice that i in the fourth loop became 4 not 3 , how is this possible . that is the question .

Tags problem

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English fares 2015-09-25 10:42:32 2209 Initial revision (published)