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 14:10
In a flashdisk wich icon can you use to open files in computer
Answers: 1
question
Computers and Technology, 22.06.2019 21:50
Answer the following questions regarding your system by using the commands listed in this chapter. for each question, write the command you used to obtain the answer. a. what are the total number of inodes in the root filesystem? how many are currently utilized? how many are available for use? b. what filesystems are currently mounted on your system? c. what filesystems are available to be mounted on your system? d. what filesystems will be automatically mounted at boot time?
Answers: 1
question
Computers and Technology, 23.06.2019 01:10
Problem 1 - hashing we would like to use initials to locate an individual. for instance, mel should locate the person mark e. lehr. note: this is all upper case. generate a hash function for the above using the numbers on your telephone. you know, each letter has a number associated with it, so examine your telephone keypad. generate 512 random 3 letter initials and take statistics on a linked list array size 512 to hold this information report how many have no elements, 1 element, 2 elements, does this agree with the hashing statistics distribution?
Answers: 1
question
Computers and Technology, 24.06.2019 02:20
Peter is thinking of a number which isless than 50. the number has 9 factors.when he adds 4 to the number, itbecomes a multiple of 5. what is thenumber he is thinking of ?
Answers: 1
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, 05.02.2020 04:54