forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_of_Sliding_Window.java
More file actions
38 lines (33 loc) · 973 Bytes
/
Copy pathMax_of_Sliding_Window.java
File metadata and controls
38 lines (33 loc) · 973 Bytes
1
2
3
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
/*You are given an array of integers,there is a sliding
window of size k which is moving from the very left of the
array to the very right Each time the sliding window moves
right by one position.Return the max sliding window.
for example:-
input: arr=[1,3,-1,-3,5,3,6,7] , k=3
output: [3,3,5,5,6,7]*/
import java.util.*;
public class Max_of_Sliding_Window {
static void printKMax(int arr[], int n, int k)
{
int j, max;
for (int i = 0; i <= n - k; i++) {
max = arr[i];
for (j = 1; j < k; j++) {
if (arr[i + j] > max)
max = arr[i + j];
}
System.out.print(max + " ");
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int k = sc.nextInt();
printKMax(arr, n, k);
}
}