Write the complete Java code and explain the code  Remove all errors from this code

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

Write the complete Java code and explain the code 

Remove all errors from this code 

import java.util.*;

import java.io.*;

 

 

class Main {

    public static void main(String args[]) {

        // taking input using class Scanner

        Scanner sc = new Scanner(System.in);

 

        // taking total count of testcases

        int t = sc.nextInt();

 

        while (t-- > 0) {

 

            // taking size of subArrays

            int k = sc.nextInt();

 

            // Declaring and Intializing an array of size n

            int arr[] = new int[n];

 

            // adding all the elements to the array

            for (int i = 0; i < n; i++) {

                arr[i] = sc.nextInt();

            }

 

            // Calling the method max_of_subarrays of class solve

            // and storing the result in an ArrayList

            ArrayList<Integer> res = new Solution().max_of_subarrays(arr, n, k);

 

            // printing the elements of the ArrayList

            for (int i = 0; i < res.size(); i++)

                System.out.print(res.get(i) + " ");

            System.out.println();

        }

    }

}

 

 

 

class Solution {

    static ArrayList<Integer> max_of_subarrays(int arr[], int n, int k) {

        

         TreeMap<Integer> map=new TreeMap<>();

        for(int i=0;i<k; ){

            map.put(arr[i],map.getOrDefault(arr[i],0)+1);

        }

        ArrayList<Integer> ;

        ans.add(map.lastKey());

        for(int i=k;i<n;i++){

            map.get(arr[i-k])-1);

            if (map.get(arr[i-k])==0)

                map.remove(arr[i-k]);

            map.put(arr[i],map.getOrDefault(arr[i],0)+1);

            ans.add(map.lastKey());

        }

        return ans

    }

}

Due to the rise of covid-19 cases in India, this year BCCI decided to organize
knock-out matches in IPL rather than a league.
Today is matchday 2 and it is between the most loved team Chennai Super Kings
and the most underrated team - Punjab Kings. Stephen Fleming, the head coach of
CSK, analyzing the batting stats of Punjab. He has stats of runs scored by all N
players in the previous season and he wants to find the maximum score for each
and every contiguous sub-list of size K to strategize for the game.
Example 1:
Input:
N = 9, K = 3
arr[] = 1 2 3 1 4 5 2 3 6
Output:
334 55 56
Explanation:
1st contiguous subarray = {1 2 3} Max = 3
2nd contiguous subarray = {2 3 1} Max = 3
3rd contiguous subarray (3 1 4] Max = 4
4th contiguous subarray = {1 4 5} Max = 5
5th contiguous subarray = {4 5 2} Max = 5
6th contiguous subarray = {5 2 3} Max = 5
7th contiguous subarray = {2 3 6} Max = 6
=
Transcribed Image Text:Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 2 and it is between the most loved team Chennai Super Kings and the most underrated team - Punjab Kings. Stephen Fleming, the head coach of CSK, analyzing the batting stats of Punjab. He has stats of runs scored by all N players in the previous season and he wants to find the maximum score for each and every contiguous sub-list of size K to strategize for the game. Example 1: Input: N = 9, K = 3 arr[] = 1 2 3 1 4 5 2 3 6 Output: 334 55 56 Explanation: 1st contiguous subarray = {1 2 3} Max = 3 2nd contiguous subarray = {2 3 1} Max = 3 3rd contiguous subarray (3 1 4] Max = 4 4th contiguous subarray = {1 4 5} Max = 5 5th contiguous subarray = {4 5 2} Max = 5 6th contiguous subarray = {5 2 3} Max = 5 7th contiguous subarray = {2 3 6} Max = 6 =
Expert Solution
steps

Step by step

Solved in 3 steps with 1 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