subject

The method countSorted is intended to find the length of the longest consecutive block of sorted values in an array, where sorted means that the next element in the array is higher than or equal to the previous element in the array. For example, if the array arr contains the values [25, 7, 7, 14, 14, 14, 21, 3, 3, 3, 5, 12, 12, 13, 13], the call countSorted(arr) should return 8, the length of the longest consecutive block of sorted numbers: 3, 3, 3, 5, 12, 12, 13, 13.

However, the method countSorted that you can see below does not work as intended.

Line 1: int countSorted(int[] array){
Line 2: int count = 1;
Line 3: int max = 1;
Line 4: for (int k = 1; k < array. length; k++) {
Line 5: if (array[k-1] <= array[k]) {
Line 6: count++;
Line 7: } else {
Line 8: if (count > max) {
Line 9: max = count;
Line 10: }
Line 11: count = 1;
Line 12: }
Line 13: }
Line 14: return max;
Line 15: }
Line 16: int [] arr = {25, 7, 7, 14, 14, 14, 21, 3, 3, 3, 5, 12, 12, 13, 13};
Line 17: System. out. println(countSorted(arr));

Enter the value printed on screen by this code segment (Line 17) ??

Which of the following changes should be made in the code so that the method works as intended?

a. It should return count instead of max
b. Before line 14, it should check if count is greater than max and, in that case, do max = count;
c. Before line 14, it should check if count is greater than max and, in that case, do max = count+1;
d. It should return max +1

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 23:30
You picked the corridor which led you here. if the guards find you, they're going to be really angry! what is the synonym of angry
Answers: 1
question
Computers and Technology, 22.06.2019 06:00
In outlook how can cherie look at the details of an event that appears on the month view of her calendar? check all that apply. by switching to the detail view by switching to the week view by switching to the day view by right-clicking on the event by double-clicking on the event by highlighting the event
Answers: 2
question
Computers and Technology, 22.06.2019 11:00
How does a policy manual an organization? a. it boost productivity. b. it create awareness in employees about the organization’s values. c. it employees achieve targets. d. it safeguards the organization from liabilities.
Answers: 1
question
Computers and Technology, 23.06.2019 00:30
Knowing that the central portion of link bd has a uniform cross sectional area of 800 mm2 , determine the magnitude of the load p for which the normal stress in link bd is 50 mpa. (hint: link bd is a two-force member.) ans: p = 62.7 kn
Answers: 2
You know the right answer?
The method countSorted is intended to find the length of the longest consecutive block of sorted val...
Questions
question
Mathematics, 26.09.2021 17:30
Questions on the website: 13722367