public class Maint static int max(int[] arr){ int index = 0; int max = arr[0]; } for(int i = 0; i < arr.length; i++){ if(max < arr[i]) { } } max = arr[i]; index = i; } return index; static int maxHeads (int[] birds, int m) { int maxHeadCut = 0; int[] arr = birds.clone(); while(m != 0) int index = max(arr); maxHeadCut + arr[index]; arr[index] = (int)Math.floor(arr[index] / 2); 8 return maxHeadCut; Run | Debug public static void main(String[] args) { int arri[] = {20, 1, 15); int arr2[] = {2, 1, 7, 4, 2); int arr3[] = {4, 10, 6, 7, 3, 1}; System.out.println("Test 1, Total Heads 20 = + maxHeads (arr1, m: 1)); //m = 1, bird 1 cut 20 System.out.println("Test 2, Total Heads 35 = " + maxHeads (arr1, m: 2)); //m = 2, System.out.println("Test 3, Total Heads 45 = " + maxHeads (arr1, m: 3)); //m = 3, bird 3 cut 15 bird 1 cut 10 System.out.println("Test 4, Total Heads 7 = " + maxHeads (arr2, m: 1)); //m = 1 System.out.println("Test 5, Total Heads 11 = " + maxHeads (arr2, m: 2)); //m = 2 System.out.println("Test 6, Total Heads 14 " + maxHeads (arr2, m: 3)); //m = 3 System.out.println("Test 7, Total Heads 10 = " + maxHeads(arr3, m: 1)); //m = 1 System.out.println("Test 8, Total Heads 17 = " + maxHeads (arr3, m: 2)); //m = 2 System.out.println("Test 9, Total Heads 23 = + maxHeads (arr3, m: 3)); //m = 3 System.out.println("Test 10, Total Heads 28 = " + maxHeads (arr3, m: 4));//m = 4 System.out.println("Test 11, Total Heads 32 = " + maxHeads (arr3, m: 5));//m = 5 System.out.println("Test 12, Total Heads 35 = " + maxHeads (arr3, m: 6));//m = 6

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How can I optimize the maxHeads function to be faster, like using a heap in this case?

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class Main{
}
static int max(int[] arr) {
int index = 0;
int max = arr [0];
}
for(int i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
index = i;
}
}
return index;
static int maxHeads (int[] birds, int m) {
int maxHeadCut = 0;
int[] arr = birds.clone();
while(m != 0)
int index = max(arr);
maxHeadCut + arr[index];
arr[index] = (int)Math.floor(arr[index] / 2);
return maxHeadCut;
Run | Debug
public static void main(String[] args) {
int arr1] = {20, 1, 15);
int arr2[] = {2, 1, 7, 4, 2};
int arr3[] = {4, 10, 6, 7, 3, 1};
System.out.println("Test 1, Total Heads 20 =
System.out.println("Test 2, Total Heads 35 =
System.out.println("Test 3, Total Heads 45 =
+ maxHeads (arr1, m: 1)); //m = 1, bird 1 cut 20 heads, arr1 is now {10,
" + maxHeads (arr1, m: 2)); //m = 2, bird 3 cut 15 head, arr1 is now (10,1
+ maxHeads (arr1, m: 3)); //m = 3, bird 1 cut 10 heads, arr1 is now (5,1
System.out.println("Test 4, Total Heads 7 = " + maxHeads (arr2, m: 1)); //m = 1
System.out.println("Test 5, Total Heads 11 = " + maxHeads (arr2, m: 2)); //m = 2
System.out.println("Test 6, Total Heads 14 = " + maxHeads (arr2, m: 3)); //m = 3
System.out.println("Test 7, Total Heads 10 =
System.out.println("Test 8, Total Heads 17 =
System.out.println("Test 9, Total Heads 23
System.out.println("Test 10, Total Heads 28 =
System.out.println("Test 11, Total Heads 32 = " +
System.out.println("Test 12, Total Heads 35 = " +
+ maxHeads (arr3, m: 1)); //m = 1
+ maxHeads (arr3, m: 2)); //m = 2
+ maxHeads (arr3, m: 3)); //m = 3
" + maxHeads (arr3, m: 4));//m = 4
maxHeads (arr3, m: 5));//m = 5
maxHeads (arr3, m: 6));//m = 6
Transcribed Image Text:4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class Main{ } static int max(int[] arr) { int index = 0; int max = arr [0]; } for(int i = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; index = i; } } return index; static int maxHeads (int[] birds, int m) { int maxHeadCut = 0; int[] arr = birds.clone(); while(m != 0) int index = max(arr); maxHeadCut + arr[index]; arr[index] = (int)Math.floor(arr[index] / 2); return maxHeadCut; Run | Debug public static void main(String[] args) { int arr1] = {20, 1, 15); int arr2[] = {2, 1, 7, 4, 2}; int arr3[] = {4, 10, 6, 7, 3, 1}; System.out.println("Test 1, Total Heads 20 = System.out.println("Test 2, Total Heads 35 = System.out.println("Test 3, Total Heads 45 = + maxHeads (arr1, m: 1)); //m = 1, bird 1 cut 20 heads, arr1 is now {10, " + maxHeads (arr1, m: 2)); //m = 2, bird 3 cut 15 head, arr1 is now (10,1 + maxHeads (arr1, m: 3)); //m = 3, bird 1 cut 10 heads, arr1 is now (5,1 System.out.println("Test 4, Total Heads 7 = " + maxHeads (arr2, m: 1)); //m = 1 System.out.println("Test 5, Total Heads 11 = " + maxHeads (arr2, m: 2)); //m = 2 System.out.println("Test 6, Total Heads 14 = " + maxHeads (arr2, m: 3)); //m = 3 System.out.println("Test 7, Total Heads 10 = System.out.println("Test 8, Total Heads 17 = System.out.println("Test 9, Total Heads 23 System.out.println("Test 10, Total Heads 28 = System.out.println("Test 11, Total Heads 32 = " + System.out.println("Test 12, Total Heads 35 = " + + maxHeads (arr3, m: 1)); //m = 1 + maxHeads (arr3, m: 2)); //m = 2 + maxHeads (arr3, m: 3)); //m = 3 " + maxHeads (arr3, m: 4));//m = 4 maxHeads (arr3, m: 5));//m = 5 maxHeads (arr3, m: 6));//m = 6
Expert Solution
Step 1

Answer:

We can optimized code if find the max element using the binary search and return the index . i hope this will help you.

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY