From bae505fc75b0ece22797b81e0fb07eeb17f90d61 Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 30 Oct 2024 11:41:40 +0600 Subject: [PATCH 1/3] Added maximum average of contiguous subarray with length k --- ...imumAverageOfContiguousSubArraysWithK.java | 53 +++++++++++++++++ ...AverageOfContiguousSubArraysWithKTest.java | 59 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java create mode 100644 src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java 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..51c631b23c70 --- /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, 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..83df3e2fd032 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java @@ -0,0 +1,59 @@ +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.58065; + double result = solution.maxAverage(k,numbers); + assertEquals(answer, result); + } +} From 04800c669280c65d7cc8dbd22cf620a4e020255d Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 30 Oct 2024 11:49:44 +0600 Subject: [PATCH 2/3] refactored code --- ...imumAverageOfContiguousSubArraysWithK.java | 2 +- ...AverageOfContiguousSubArraysWithKTest.java | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java index 51c631b23c70..b28e0ff17d8b 100644 --- a/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java +++ b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java @@ -38,7 +38,7 @@ public double maxAverage(int k, int... numbers) { 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) { + 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 diff --git a/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java b/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java index 83df3e2fd032..6cc14e43c58b 100644 --- a/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java +++ b/src/test/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithKTest.java @@ -19,7 +19,7 @@ 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); + double result = solution.maxAverage(k, numbers); assertEquals(answer, result); } @@ -28,7 +28,7 @@ public void testCaseTwoOneElement() { int[] numbers = new int[] {5}; int k = 1; double answer = 5.00; - double result = solution.maxAverage(k,numbers); + double result = solution.maxAverage(k, numbers); assertEquals(answer, result); } @@ -37,23 +37,16 @@ public void testCaseThreeNegativeElement() { int[] numbers = new int[] {-1}; int k = 1; double answer = -1.00; - double result = solution.maxAverage(k,numbers); + 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[] 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.58065; - double result = solution.maxAverage(k,numbers); + double answer = -594.5806451612904; + double result = solution.maxAverage(k, numbers); assertEquals(answer, result); } } From 587edef534c3bead54ff40b8502e14419eaae250 Mon Sep 17 00:00:00 2001 From: S-M-J-I Date: Wed, 30 Oct 2024 12:07:16 +0600 Subject: [PATCH 3/3] jobs not starting --- .../others/MaximumAverageOfContiguousSubArraysWithK.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java index b28e0ff17d8b..9938121381ac 100644 --- a/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java +++ b/src/main/java/com/thealgorithms/others/MaximumAverageOfContiguousSubArraysWithK.java @@ -22,7 +22,7 @@ public MaximumAverageOfContiguousSubArraysWithK() { * @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, returns 0. + * @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