Skip to content

Commit c5e1a75

Browse files
author
Dhananjay Nagargoje
committed
siliding window
1 parent d551117 commit c5e1a75

File tree

6 files changed

+209
-108
lines changed

6 files changed

+209
-108
lines changed

Diff for: .idea/workspace.xml

+88-108
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.24 KB
Binary file not shown.

Diff for: out/production/main/problems/onmath/gfg/Sieve.class

1.33 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problems.patterns.slidingwindows;
2+
import java.util.Arrays;
3+
4+
class AverageOfSubarrayOfSizeK {
5+
public static double[] findAverages(int K, int[] arr) {
6+
double[] result = new double[arr.length - K + 1];
7+
double windowSum = 0;
8+
int windowStart = 0;
9+
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
10+
windowSum += arr[windowEnd]; // add the next element
11+
// slide the window, we don't need to slide if we've not hit the required window size of 'k'
12+
if (windowEnd >= K - 1) {
13+
result[windowStart] = windowSum / K; // calculate the average
14+
windowSum -= arr[windowStart]; // subtract the element going out
15+
windowStart++; // slide the window ahead
16+
}
17+
}
18+
19+
return result;
20+
}
21+
22+
public static void main(String[] args) {
23+
double[] result = AverageOfSubarrayOfSizeK.findAverages(5, new int[] { 1, 3, 2, 6, -1, 4, 1, 8, 2 });
24+
System.out.println("Averages of subarrays of size K: " + Arrays.toString(result));
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package problems.patterns.slidingwindows;
2+
class MaxSumSubArrayOfSizeK {
3+
/**
4+
* Given an array of positive numbers and a positive number ‘k’, find the maximum sum of any contiguous subarray of size ‘k’.
5+
*
6+
* Example 1:
7+
*
8+
* Input: [2, 1, 5, 1, 3, 2], k=3
9+
* Output: 9
10+
* Explanation: Subarray with maximum sum is [5, 1, 3].
11+
* Example 2:
12+
*
13+
* Input: [2, 3, 4, 1, 5], k=2
14+
* Output: 7
15+
* Explanation: Subarray with maximum sum is [3, 4].
16+
* @param k
17+
* @param arr
18+
* @return
19+
*/
20+
21+
22+
public static int findMaxSumSubArray(int k, int[] arr) {
23+
int windowSum = 0, maxSum = 0;
24+
int windowStart = 0;
25+
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
26+
windowSum += arr[windowEnd]; // add the next element
27+
// slide the window, we don't need to slide if we've not hit the required window size of 'k'
28+
if (windowEnd >= k - 1) {
29+
maxSum = Math.max(maxSum, windowSum);
30+
windowSum -= arr[windowStart]; // subtract the element going out
31+
windowStart++; // slide the window ahead
32+
}
33+
}
34+
35+
return maxSum;
36+
}
37+
38+
public static void main(String[] args) {
39+
System.out.println("Maximum sum of a subarray of size K: "
40+
+ MaxSumSubArrayOfSizeK.findMaxSumSubArray(3, new int[] { 2, 1, 5, 1, 3, 2 }));
41+
System.out.println("Maximum sum of a subarray of size K: "
42+
+ MaxSumSubArrayOfSizeK.findMaxSumSubArray(2, new int[] { 2, 3, 4, 1, 5 }));
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problems.patterns.slidingwindows;
2+
class MinSizeSubArraySum {
3+
4+
/**
5+
* Given an array of positive numbers and a positive number ‘S’, find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. Return 0, if no such subarray exists.
6+
*
7+
* Example 1:
8+
*
9+
* Input: [2, 1, 5, 2, 3, 2], S=7
10+
* Output: 2
11+
* Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2].
12+
* Example 2:
13+
*
14+
* Input: [2, 1, 5, 2, 8], S=7
15+
* Output: 1
16+
* Explanation: The smallest subarray with a sum greater than or equal to '7' is [8].
17+
* Example 3:
18+
*
19+
* Input: [3, 4, 1, 1, 6], S=8
20+
* Output: 3
21+
* Explanation: Smallest subarrays with a sum greater than or equal to '8' are [3, 4, 1] or [1, 1, 6].
22+
* @param S
23+
* @param arr
24+
* @return
25+
*/
26+
27+
public static int findMinSubArray(int S, int[] arr) {
28+
int windowSum = 0, minLength = Integer.MAX_VALUE;
29+
int windowStart = 0;
30+
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
31+
windowSum += arr[windowEnd]; // add the next element
32+
// shrink the window as small as possible until the 'windowSum' is smaller than 'S'
33+
while (windowSum >= S) {
34+
minLength = Math.min(minLength, windowEnd - windowStart + 1);
35+
windowSum -= arr[windowStart]; // subtract the element going out
36+
windowStart++; // slide the window ahead
37+
}
38+
}
39+
40+
return minLength == Integer.MAX_VALUE ? 0 : minLength;
41+
}
42+
43+
public static void main(String[] args) {
44+
int result = MinSizeSubArraySum.findMinSubArray(7, new int[] { 2, 1, 5, 2, 3, 2 });
45+
System.out.println("Smallest subarray length: " + result);
46+
result = MinSizeSubArraySum.findMinSubArray(7, new int[] { 2, 1, 5, 2, 8 });
47+
System.out.println("Smallest subarray length: " + result);
48+
result = MinSizeSubArraySum.findMinSubArray(8, new int[] { 3, 4, 1, 1, 6 });
49+
System.out.println("Smallest subarray length: " + result);
50+
}
51+
}

0 commit comments

Comments
 (0)