Skip to content

Commit bae505f

Browse files
committed
Added maximum average of contiguous subarray with length k
1 parent 03bb8ee commit bae505f

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.others;
2+
3+
/**
4+
* References: https://www.geeksforgeeks.org/find-maximum-average-subarray-of-k-length/
5+
*
6+
* This algorithm involves computing the maximum average of sub arrays of a fixed size k from integers.
7+
* This is a sliding window technique
8+
* As we go through the array, we sum up the integers upto k.
9+
* Once we do reach k initially, we average the numbers and compare it to the current maximum
10+
* To move onto the next number, we subtract the number at the start of the window,
11+
* and add the number at the end of the window
12+
*
13+
* @author S-M-J-I
14+
*/
15+
public class MaximumAverageOfContiguousSubArraysWithK {
16+
public MaximumAverageOfContiguousSubArraysWithK() {
17+
}
18+
19+
/**
20+
* Finds the maximum average of a contiguous sub array of size K.
21+
*
22+
* @param k The size of the sub array.
23+
* @param numbers The array from which sub arrays will be considered.
24+
*
25+
* @return The maximum average of the contiguous sub array of size K. If no such sub array exists, returns 0.
26+
*/
27+
public double maxAverage(int k, int... numbers) {
28+
// If there are no elements in the array
29+
if (numbers.length == 0) {
30+
// Return 0
31+
return 0;
32+
}
33+
// If there is only one element, return that element
34+
if (numbers.length == 1) {
35+
return numbers[0];
36+
}
37+
38+
double MAX = Integer.MIN_VALUE; // Keep track of the maximum average
39+
double runningSum = 0; // Keep track of the running sum
40+
// For all elements in the array
41+
for(int i = 0; i < numbers.length; ++i) {
42+
runningSum += numbers[i]; // Add the current element to the running sum
43+
if (i >= k - 1) { // If we reach a minimum number of k elements
44+
double div = runningSum / k; // Find the average
45+
MAX = Math.max(div, MAX); // Compare and set the new maximum
46+
runningSum -= numbers[i - (k - 1)]; // Subtract the first element in the window
47+
}
48+
}
49+
50+
// Return the maximum average
51+
return MAX;
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class MaximumAverageOfContiguousSubArraysWithKTest {
9+
10+
private MaximumAverageOfContiguousSubArraysWithK solution;
11+
12+
@BeforeEach
13+
void setUp() {
14+
solution = new MaximumAverageOfContiguousSubArraysWithK();
15+
}
16+
17+
@Test
18+
public void testCaseOneNormal() {
19+
int[] numbers = new int[] {1,12,-5,-6,50,3};
20+
int k = 4;
21+
double answer = 12.75;
22+
double result = solution.maxAverage(k,numbers);
23+
assertEquals(answer, result);
24+
}
25+
26+
@Test
27+
public void testCaseTwoOneElement() {
28+
int[] numbers = new int[] {5};
29+
int k = 1;
30+
double answer = 5.00;
31+
double result = solution.maxAverage(k,numbers);
32+
assertEquals(answer, result);
33+
}
34+
35+
@Test
36+
public void testCaseThreeNegativeElement() {
37+
int[] numbers = new int[] {-1};
38+
int k = 1;
39+
double answer = -1.00;
40+
double result = solution.maxAverage(k,numbers);
41+
assertEquals(answer, result);
42+
}
43+
44+
@Test
45+
public void testCaseFourNearLimit() {
46+
int[] numbers = new int[] {
47+
8860,-853,6534,4477,-4589,8646,-6155,-5577,-1656,-5779,-2619,-8604,-1358,-8009,
48+
4983,7063,3104,-1560,4080,2763,5616,-2375,2848,1394,-7173,-5225,-8244,-809,8025,
49+
-4072,-4391,-9579,1407,6700,2421,-6685,5481,-1732,-8892,-6645,3077,3287,-4149,8701,
50+
-4393,-9070,-1777,2237,-3253,-506,-4931,-7366,-8132,5406,-6300,-275,-1908,67,3569,1433,
51+
-7262,-437,8303,4498,-379,3054,-6285,4203,6908,4433,3077,2288,9733,-8067,3007,9725,9669,
52+
1362,-2561,-4225,5442,-9006,-429,160,-9234,-4444,3586,-5711,-9506,-79,-4418,-4348,-5891
53+
};
54+
int k = 93;
55+
double answer = -594.58065;
56+
double result = solution.maxAverage(k,numbers);
57+
assertEquals(answer, result);
58+
}
59+
}

0 commit comments

Comments
 (0)