-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MaximumEachSubarray.java
105 lines (85 loc) · 3.34 KB
/
MaximumEachSubarray.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Maximum of All Subarrays of Size k
* Java program to find the maximum integer in each subarray of
* fixed size k, given an array of integers, using the 'Sliding
* Window Technique'.
*/
import java.io.*;
import java.util.*;
public class MaximumEachSubarray {
private static String findMaxEachSubarray(int[] numArray, int k) {
String result = "";
//ArrayDeque stores indices of useful elements for each window.
//These indices are sorted in decreasing order, so that for each
//window, index of maximum element is at the front.
ArrayDeque<Integer> indices = new ArrayDeque<>();
//For first window
for(int i=0; i<k; i++){
//For any element, all elements smaller than it and to its left in
//the window are useless as they cannot be the maximum for that
//window. Hence, we remove them from rear of deque.
while(!indices.isEmpty() && (numArray[i] >= numArray[indices.peekLast()]))
indices.removeLast();
//Adding newest element to rear of deque
indices.addLast(i);
}
//For first window
result += (numArray[indices.peek()] + " ");
//The same process is repeated over all windows of length k.
for(int i=k; i<numArray.length; i++) {
//If window has crossed the index at front of deque
if(indices.peek() <= (i-k))
indices.removeFirst();
while(!indices.isEmpty() && (numArray[indices.peekLast()] <= numArray[i]))
indices.removeLast();
indices.addLast(i);
result += (numArray[indices.peek()] + " ");
}
return result;
}
public static void main(String[] args) throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(read);
//Taking input from user
System.out.println("Enter length of array of integers: ");
int len = Integer.parseInt(buf.readLine());
System.out.println("Enter array of integers: ");
StringTokenizer st = new StringTokenizer(buf.readLine());
int numArray[] = new int[len];
for(int i=0; i<len; i++) {
numArray[i] = Integer.parseInt(st.nextToken());
}
System.out.println("Enter size of window: ");
int K = Integer.parseInt(buf.readLine());
//Output is a String in which the first integer is maximum for 1st K-sized window,
//the second integer is maximum for 2nd K-sized window, and so on.
System.out.println("String of maximum integers in all K-sized subarrays: ");
System.out.println(findMaxEachSubarray(numArray, K));
}
}
/*
Time complexity: O(N)
Space complexity: O(k) (ArrayDeque used to store indices can be,
at most, k in length)
TEST CASES
INPUT
Enter length of array of integers:
10
Enter array of integers:
12 3 4 5 1 7 2 9 1 18
Enter size of window:
4
OUTPUT
String of maximum integers in all K-sized subarrays:
12 5 7 7 9 9 18
INPUT
Enter length of array of integers:
8
Enter array of integers:
1 3 -1 -3 5 3 6 7
Enter size of window:
3
OUTPUT
String of maximum integers in all K-sized subarrays:
3 3 5 5 6 7
*/