Write the complete Java code and explain the code Remove all errors from this code
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
=](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2a3320cb-d3af-421c-9a73-c7567aaab1e6%2F4068b00f-bd42-4758-8926-246f9e1ee587%2F3yfqguv_processed.jpeg&w=3840&q=75)

Step by step
Solved in 3 steps with 1 images









