问题描述:

I have a problem that asks:

Write a program that converts its input arguments into an array of

integers, and then finds the length and location of the longest

contiguous sequence of equal values where the values of the elements

just before and just after this sequence are smaller.

For example, if

the command line arguments are “1 2 2 2 2 5 5 5 3” your program should

output the numbers 5 3 (the first number is a zero-based offset, and

the second number is the length of the subsequence). If a contiguous

subsequence appears at the beginning or end of the array, treat this

as a special case;e.g.,for input “5 5 5 5 3 8 8 8 1”your output should

be 0 4 (and not 5 3). If there are multiple subsequences that satisfy

the above condition, then output the first one.

Updated code:

`public class LongestPlateau {`

public static void main(String[] args) {

// TODO - Your solution

int N= args.length;

int [] array = new int [N];

int new_length=0;

int location=0;

int max=0;

int current_length=0;

//assign digits into array

for (int i=0; i < N; i++){

int number = Integer.parseInt(args[i]);

array [i] = number;

}

int compare=array[0];

for (int l=0; l<N; l++){

if (array[l] < compare){

current_length=0;

compare = array[l];

}

else if (array[l] == compare){

current_length+=1;

compare = array[l];

}

else if (array[l] > compare){

compare=array[l];

l++;

}

compare= array[l];

for (int b=0; b<N; b++){

if (current_length > max){

max = current_length;

location = array[l];

new_length=max-1;

}

else if (current_length==1){

new_length=max;

}

}

}

System.out.println(location);

System.out.println(new_length);

}

}

Issue is that for the input of "1 2 3 4" I continously get an Array Index out of bounds error.

Before you start writing code, try and think how a human would have solved it.

e.g.

For every item in the input, compare it to the previous, if it's larger, start a new sequence length check (write 0 in your notebook under - "current sequence length)), if it's the same, increase it by 1, if it's less, mark that sequence length as complete. if it's larger than your largest sequence length so far (started with 0) then this is now your largest sequence, if not, ignore that sequence length and move on to the next character. (or something like this)

write these instructions to yourself as a human, and try to follow them, and fix them as you find edge cases. Once you have a working human language algorithm, writing the code will be almost self driven.

You really need to post the specific issue you are seeing, how your actual results differ from your expected results, and what solutions you have attempted.

In any case, as for the general "how to proceed" question, I find that it often helps to work out these types of problems on paper first. Write down your sequence and step through it, observe what information you need to keep track of and what logic you need to apply to produce the desired results. Once you are able to do this, it will be far more straightforward to translate your clearly thought out algorithm into concrete code.

It appears you are at least somewhat on the right track parsing and storing your integer array, but you are a bit misguided with your `[t+?]`

lookaheads. If you write this out and step through it by hand, you may be surprised at what you come up with.