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
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...
Related questions
Question
How can I optimize the maxHeads function to be faster, like using a heap in this case?
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.
Step by step
Solved in 5 steps with 2 images
Recommended textbooks for you
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
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
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY