From 3a6abbf51ae17efd86576f48b35a752746476e17 Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:10:13 +0200 Subject: [PATCH 1/6] feat: added new slidingwindow folder, configured CMakeLists --- CMakeLists.txt | 1 + slidingwindow/CMakeLists.txt | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 slidingwindow/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fb339e475f..2d3db4ca367 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ add_subdirectory(physics) add_subdirectory(probability) add_subdirectory(range_queries) add_subdirectory(search) +add_subdirectory(slidingwindow) add_subdirectory(sorting) add_subdirectory(strings) diff --git a/slidingwindow/CMakeLists.txt b/slidingwindow/CMakeLists.txt new file mode 100644 index 00000000000..01e8c3a0d98 --- /dev/null +++ b/slidingwindow/CMakeLists.txt @@ -0,0 +1,16 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. The RELATIVE flag makes it easier to extract an executable's name +# automatically. + +file( GLOB APP_SOURCES RELATIVE . *.cpp ) +foreach( testsourcefile ${APP_SOURCES} ) + string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp` + add_executable( ${testname} ${testsourcefile} ) + + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + if(OpenMP_CXX_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${testname} DESTINATION "bin/slidingwindow") # Folder name. Do NOT include `<>` + +endforeach( testsourcefile ${APP_SOURCES} ) \ No newline at end of file From 85c1132adfdb67ea8904722de4d475829f0ea8bf Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:15:28 +0200 Subject: [PATCH 2/6] feat: implemented MaxSumKSizeSubarray algorithm --- slidingwindow/MaxSumKSizeSubarray.cpp | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 slidingwindow/MaxSumKSizeSubarray.cpp diff --git a/slidingwindow/MaxSumKSizeSubarray.cpp b/slidingwindow/MaxSumKSizeSubarray.cpp new file mode 100644 index 00000000000..5e9f2162b94 --- /dev/null +++ b/slidingwindow/MaxSumKSizeSubarray.cpp @@ -0,0 +1,34 @@ +#include +#include + +namespace slidingwindow { + +int maxSumKSizeSubarray(const std::vector& array, const size_t k) { + // terminate if the array size is smaller than the size of the subarray + if (array.size() < k) { + return INT_MIN; + } + + int max_sum = 0; + int window_sum = 0; + + // sum of the first k elements + for (size_t i = 0; i < k; i++) { + max_sum += array[i]; + } + + window_sum = max_sum; + + // sliding the window + for (size_t i = k; i < array.size(); i++) { + window_sum += array[i] - array[i - k]; + if (window_sum > max_sum) max_sum = window_sum; + } + + return max_sum; +} +} + +int main() { + return 0; +} \ No newline at end of file From b79c22c14415852545aa8fbf8d668eff5fb58f6c Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:18:23 +0200 Subject: [PATCH 3/6] test: added tests for algorithms --- slidingwindow/MaxSumKSizeSubarray.cpp | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/slidingwindow/MaxSumKSizeSubarray.cpp b/slidingwindow/MaxSumKSizeSubarray.cpp index 5e9f2162b94..27f12bda14a 100644 --- a/slidingwindow/MaxSumKSizeSubarray.cpp +++ b/slidingwindow/MaxSumKSizeSubarray.cpp @@ -29,6 +29,37 @@ int maxSumKSizeSubarray(const std::vector& array, const size_t k) { } } +void test() { + const std::vector arr_1 = {3, 4, -3, 0, 9, 3, -2, 7}; + const int sum_1 = slidingwindow::maxSumKSizeSubarray(arr_1, 3); + const int sum_2 = slidingwindow::maxSumKSizeSubarray(arr_1, 4); + const int sum_3 = slidingwindow::maxSumKSizeSubarray(arr_1, 5); + const int sum_4 = slidingwindow::maxSumKSizeSubarray(arr_1, 6); + + assert(sum_1 == 12); + assert(sum_2 == 17); + assert(sum_3 == 17); + assert(sum_4 == 16); + + const std::vector arr_2 = {1, -2, 5, 6, -1, 8, 2, -3}; + const int sum_5 = slidingwindow::maxSumKSizeSubarray(arr_2, 3); + const int sum_6 = slidingwindow::maxSumKSizeSubarray(arr_2, 4); + const int sum_7 = slidingwindow::maxSumKSizeSubarray(arr_2, 5); + const int sum_8 = slidingwindow::maxSumKSizeSubarray(arr_2, 6); + + assert(sum_5 == 13); + assert(sum_6 == 18); + assert(sum_7 == 20); + assert(sum_8 == 18); + + // Case: k > array Size + const std::vector arr_3 = {8, 5, -1, 0}; + const int sum_9 =slidingwindow::maxSumKSizeSubarray(arr_3, 6); + + assert(sum_9 == INT_MIN); +} + int main() { + test(); return 0; } \ No newline at end of file From f55fe66da928d6e9d6f53949057cfd3681e81684 Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:20:12 +0200 Subject: [PATCH 4/6] docs: added documentation for the algorithm --- slidingwindow/MaxSumKSizeSubarray.cpp | 37 ++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/slidingwindow/MaxSumKSizeSubarray.cpp b/slidingwindow/MaxSumKSizeSubarray.cpp index 27f12bda14a..9106dcc029f 100644 --- a/slidingwindow/MaxSumKSizeSubarray.cpp +++ b/slidingwindow/MaxSumKSizeSubarray.cpp @@ -1,8 +1,37 @@ +/** + * @brief Calculation of the maximum Sum of a k-sized subarray + * + * @details + * This algorithm uses the sliding window technique to efficiently compute the + * maximum sum of a subarray of size k. Through reusing the results from previous + * calculations it reduces the time complexity from O(n*k) to O(n). This is + * especially more efficient when dealing with large input arrays. + * + * In case of a subarray size that is smaller than the arrays size, the algorithm + * terminates instantly. + * + * Time Complexity: + * Worst-case O(n) + * Average-case O(n) + * Best-case O(n) + */ + #include #include +/** + * @namespace slidingwindow + * @brief slidingwindow algorithms + */ namespace slidingwindow { +/** + * Calculates the maximum sum of a subarray with size k + * + * @param array Array of which the maximum of its subarray should be calculated + * @param k Size of the subarray + * @return the calculated maximum sum of the subarray + */ int maxSumKSizeSubarray(const std::vector& array, const size_t k) { // terminate if the array size is smaller than the size of the subarray if (array.size() < k) { @@ -27,8 +56,11 @@ int maxSumKSizeSubarray(const std::vector& array, const size_t k) { return max_sum; } -} +} // namespace slidingwindow +/** + * @brief self tested implementation + */ void test() { const std::vector arr_1 = {3, 4, -3, 0, 9, 3, -2, 7}; const int sum_1 = slidingwindow::maxSumKSizeSubarray(arr_1, 3); @@ -59,6 +91,9 @@ void test() { assert(sum_9 == INT_MIN); } +/** + * @brief Main function + */ int main() { test(); return 0; From 335d08c94be8d2587bbb0bd1f1ac3e88f211ee47 Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:46:07 +0200 Subject: [PATCH 5/6] docs: added missing link to more explanations --- slidingwindow/MaxSumKSizeSubarray.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/slidingwindow/MaxSumKSizeSubarray.cpp b/slidingwindow/MaxSumKSizeSubarray.cpp index 9106dcc029f..dd232e27163 100644 --- a/slidingwindow/MaxSumKSizeSubarray.cpp +++ b/slidingwindow/MaxSumKSizeSubarray.cpp @@ -14,6 +14,9 @@ * Worst-case O(n) * Average-case O(n) * Best-case O(n) + * + * For more information about this topic, there is more here: + * https://www.geeksforgeeks.org/dsa/window-sliding-technique/ */ #include From 49df788ff1f23f7c7ddcb983b358e6dca5100193 Mon Sep 17 00:00:00 2001 From: HalberBach Date: Tue, 31 Mar 2026 18:48:49 +0200 Subject: [PATCH 6/6] chore: renamed file and method to stay with standards --- .../{MaxSumKSizeSubarray.cpp => max_sum_k_size_subarray.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename slidingwindow/{MaxSumKSizeSubarray.cpp => max_sum_k_size_subarray.cpp} (97%) diff --git a/slidingwindow/MaxSumKSizeSubarray.cpp b/slidingwindow/max_sum_k_size_subarray.cpp similarity index 97% rename from slidingwindow/MaxSumKSizeSubarray.cpp rename to slidingwindow/max_sum_k_size_subarray.cpp index dd232e27163..11be4d8a5a1 100644 --- a/slidingwindow/MaxSumKSizeSubarray.cpp +++ b/slidingwindow/max_sum_k_size_subarray.cpp @@ -35,7 +35,7 @@ namespace slidingwindow { * @param k Size of the subarray * @return the calculated maximum sum of the subarray */ -int maxSumKSizeSubarray(const std::vector& array, const size_t k) { +int max_sum_k_size_subarray(const std::vector& array, const size_t k) { // terminate if the array size is smaller than the size of the subarray if (array.size() < k) { return INT_MIN;