The following method should returns the length of the longest sorted sequence within a list of integers. For example, if a variable called list stores the following sequence of values: {11,12,30,41,5,3,7,6}, it should return 4.
When the longest sorted sequence starts at front, this method fails the test(it returns 3), but it works for the other tests. Does anybody know where the problem is? Thank you.
public int longestSortedSequence() {
int count = 0;
int max1 = 0;
int max2 = 0;
for (int i = 0; i < size; i++) {
if (elementData[i] <= elementData[i + 1]) {
count++;
if (count >= max1) {
max1 = count;
}
} else if (elementData[i] > elementData[i + 1]) {
count = 0;
count++;
if (count >= max2) {
max2 = count;
}
}
}
return Math.max(max1, max2);
}