Question: The code i have in Java should perform the following task: My code below is supposed to perform this task but when I run the

The code i have in Java should perform the following task:

The code i have in Java should perform the following task: My

My code below is supposed to perform this task but when I run the test cases, most of them fail. Can you please tell me what my error is?

CODE

public static int longestSeq (int[] array) {

if (array.length == 0) {

return 0;

}

int maxSeq = 0;

int counter = 0;

for (int i = 0; i

if (array[i] == array[i-1] + 1) {

counter++;

if (counter > maxSeq) {

maxSeq = counter;

}

else {

// reset counter

counter = 0;

}}}

return maxSeq; }

TEST CASES

code below is supposed to perform this task but when I run

Write a method named longestSeq that takes as a parameter an array if integers. The method should return the length of the longest consecutive sequence of integers in the array. For example, if the array has the following values: [7,9,10,3,2,1,12,20], there are 2 consecutive sequences (9,10),(3,2,1). The longest one is the second sequence; hence the method should return 3. If there are no consecutive sequences in the array, the method should return 1 . If the array is empty the return value is 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!