Question: Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of

Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. (The same code is used in another question.) For example, if the array nums contains the values [7,10,10,15,15,15,15,10,10,10,15,10,10], the call find- Longest(10) should return 3, the length of the longest consecutive block of 10s. 1 private int[] nums; 2 public int findLongest(int target) { 3 int lenCount = 0; 4 int maxLen = 0; 5 for (int i = 0; i < nums.length; i++) { 6 if (nums[i] == target) { 7 lenCount++; 8 } else { 9 if (lenCount > maxLen) { 10 maxLen = lenCount; 11 } 12 } 13 } 14 if (lenCount > maxLen) { 15 maxLen = lenCount; 16 } 17 return maxLen; 18 }

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 Programming Questions!