diff --git a/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java new file mode 100644 index 000000000000..9938121381ac --- /dev/null +++ b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java @@ -0,0 +1,53 @@ +package com.thealgorithms.others; + +/** + * References: https://www.geeksforgeeks.org/find-maximum-average-subarray-of-k-length/ + * + * This algorithm involves computing the maximum average of sub arrays of a fixed size k from integers. + * This is a sliding window technique + * As we go through the array, we sum up the integers upto k. + * Once we do reach k initially, we average the numbers and compare it to the current maximum + * To move onto the next number, we subtract the number at the start of the window, + * and add the number at the end of the window + * + * @author S-M-J-I + */ +public class MaximumAverageOfContiguousSubArraysWithK { + public MaximumAverageOfContiguousSubArraysWithK() { + } + + /** + * Finds the maximum average of a contiguous sub array of size K. + * + * @param k The size of the sub array. + * @param numbers The array from which sub arrays will be considered. + * + * @return The maximum average of the contiguous sub array of size K. If no such sub array exists, then it returns 0. + */ + public double maxAverage(int k, int... numbers) { + // If there are no elements in the array + if (numbers.length == 0) { + // Return 0 + return 0; + } + // If there is only one element, return that element + if (numbers.length == 1) { + return numbers[0]; + } + + double MAX = Integer.MIN_VALUE; // Keep track of the maximum average + double runningSum = 0; // Keep track of the running sum + // For all elements in the array + for (int i = 0; i < numbers.length; ++i) { + runningSum += numbers[i]; // Add the current element to the running sum + if (i >= k - 1) { // If we reach a minimum number of k elements + double div = runningSum / k; // Find the average + MAX = Math.max(div, MAX); // Compare and set the new maximum + runningSum -= numbers[i - (k - 1)]; // Subtract the first element in the window + } + } + + // Return the maximum average + return MAX; + } +} diff --git a/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java b/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java new file mode 100644 index 000000000000..6cc14e43c58b --- /dev/null +++ b/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java @@ -0,0 +1,52 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class MaximumAverageOfContiguousSubArraysWithKTest { + + private MaximumAverageOfContiguousSubArraysWithK solution; + + @BeforeEach + void setUp() { + solution = new MaximumAverageOfContiguousSubArraysWithK(); + } + + @Test + public void testCaseOneNormal() { + int[] numbers = new int[] {1,12,-5,-6,50,3}; + int k = 4; + double answer = 12.75; + double result = solution.maxAverage(k, numbers); + assertEquals(answer, result); + } + + @Test + public void testCaseTwoOneElement() { + int[] numbers = new int[] {5}; + int k = 1; + double answer = 5.00; + double result = solution.maxAverage(k, numbers); + assertEquals(answer, result); + } + + @Test + public void testCaseThreeNegativeElement() { + int[] numbers = new int[] {-1}; + int k = 1; + double answer = -1.00; + double result = solution.maxAverage(k, numbers); + assertEquals(answer, result); + } + + @Test + public void testCaseFourNearLimit() { + int[] numbers = new int[] {8860,-853,6534,4477,-4589,8646,-6155,-5577,-1656,-5779,-2619,-8604,-1358,-8009,4983,7063,3104,-1560,4080,2763,5616,-2375,2848,1394,-7173,-5225,-8244,-809,8025,-4072,-4391,-9579,1407,6700,2421,-6685,5481,-1732,-8892,-6645,3077,3287,-4149,8701,-4393,-9070,-1777,2237,-3253,-506,-4931,-7366,-8132,5406,-6300,-275,-1908,67,3569,1433,-7262,-437,8303,4498,-379,3054,-6285,4203,6908,4433,3077,2288,9733,-8067,3007,9725,9669,1362,-2561,-4225,5442,-9006,-429,160,-9234,-4444,3586,-5711,-9506,-79,-4418,-4348,-5891}; + int k = 93; + double answer = -594.5806451612904; + double result = solution.maxAverage(k, numbers); + assertEquals(answer, result); + } +}