From f99f0d6e21248cd0f51027dd98e9515b01bf7e93 Mon Sep 17 00:00:00 2001 From: DataWorshipper Date: Thu, 2 Oct 2025 01:26:38 +0530 Subject: [PATCH 1/6] Add sliding window XOR implementation with tests in C++ --- bit_manipulation/sliding_window_xor.cpp | 130 ++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 bit_manipulation/sliding_window_xor.cpp diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp new file mode 100644 index 0000000000..829cec4a6e --- /dev/null +++ b/bit_manipulation/sliding_window_xor.cpp @@ -0,0 +1,130 @@ +/** + * @file + * @brief Implementation to [calculate XOR of sliding window of size k in an array of n integers] + * (https://cses.fi/problemset/task/3426) + * + * @details + * We are given an array of n integers. Our task is to calculate the bitwise XOR of each + * window of k elements, from left to right, and cumulatively XOR the results into a single value. + * + * Worst Case Time Complexity: O(n) + * Space Complexity: O(n) + * + * @author [Abhiraj Mandal](https://github.com/DataWorshipper) + */ + +#include /// for assert +#include +#include +#include /// for IO operations + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace sliding_window_xor + * @brief Functions for cumulative XOR of sliding windows in arrays + */ +namespace sliding_window_xor { + +/** + * @brief Computes cumulative XOR of all windows of size k + * + * @param n Size of the array + * @param k Window size + * @param x Initial value to generate the array + * @param a Multiplier in array generation + * @param b Increment in array generation + * @param c Modulo in array generation + * @returns std::uint64_t The cumulative XOR of all windows of size k + * + * @details + * This function generates the array using the recurrence: + * arr[0] = x + * arr[i] = (a * arr[i-1] + b) % c + * + * It maintains a sliding window of size k using two pointers l and r: + * - x1 stores the XOR of the current window + * - x2 stores the cumulative XOR of all valid windows + * + * This approach ensures that the algorithm runs in O(n) time. + */ +std::uint64_t compute( + std::uint64_t n, + std::uint64_t k, + std::uint64_t x, + std::uint64_t a, + std::uint64_t b, + std::uint64_t c) { + + // Generate the array of n elements + std::vector arr(n); + arr[0] = x; // First element of the array + + for (std::uint64_t i = 1; i < n; ++i) { + arr[i] = (a * arr[i - 1] + b) % c; // recurrence relation + } + + std::uint64_t x1 = 0; // XOR of the current window + std::uint64_t x2 = 0; // Cumulative XOR of all windows of size k + std::uint64_t l = 0; // Left pointer of sliding window + std::uint64_t r = 0; // Right pointer of sliding window + + // Slide the window over the array + while (r < n) { + x1 ^= arr[r]; // include current element in window XOR + + // Shrink window from left if size exceeds k + while (r - l + 1 > k) { + x1 ^= arr[l]; // remove leftmost element from window XOR + ++l; + } + + // If window size equals k, add it to cumulative XOR + if (r - l + 1 == k) { + x2 ^= x1; + } + + ++r; // Move right pointer + } + + return x2; // Return cumulative XOR of all windows +} + +} // namespace sliding_window_xor +} // namespace bit_manipulation + +/** + * @brief Self-test implementation + */ +static void test() { + using bit_manipulation::sliding_window_xor::compute; + + // Testcase 1: n = 100, k = 20, expected = 1019 + assert(compute(100, 20, 3, 7, 1, 997) == 1019); + + // Testcase 2: n = 2, k = 1, expected = 2 + assert(compute(2, 1, 2, 3, 4, 5) == 2); + + // Testcase 3: n = 5, k = 2 + assert(compute(5, 2, 1, 1, 1, 100) == 0 ^ 3 ^ 1 ^ 7); + + // Testcase 4: n = 3, k = 5, expected = 0 + assert(compute(3, 5, 5, 2, 1, 100) == 0); + + // Testcase 5: n = 4, k = 4, expected = 0 + assert(compute(4, 4, 3, 1, 0, 10) == 0); + + std::cout << "All test cases successfully passed!" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} \ No newline at end of file From a285fc8735ed09dcd00d0a45a121784e1f077508 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 2 Oct 2025 05:15:25 +0000 Subject: [PATCH 2/6] clang-format and clang-tidy fixes for f99f0d6e --- bit_manipulation/sliding_window_xor.cpp | 39 +++++++++++-------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp index 829cec4a6e..2843de51b8 100644 --- a/bit_manipulation/sliding_window_xor.cpp +++ b/bit_manipulation/sliding_window_xor.cpp @@ -1,22 +1,23 @@ /** * @file - * @brief Implementation to [calculate XOR of sliding window of size k in an array of n integers] - * (https://cses.fi/problemset/task/3426) + * @brief Implementation to [calculate XOR of sliding window of size k in an + * array of n integers] (https://cses.fi/problemset/task/3426) * * @details - * We are given an array of n integers. Our task is to calculate the bitwise XOR of each - * window of k elements, from left to right, and cumulatively XOR the results into a single value. - * + * We are given an array of n integers. Our task is to calculate the bitwise XOR + * of each window of k elements, from left to right, and cumulatively XOR the + * results into a single value. + * * Worst Case Time Complexity: O(n) * Space Complexity: O(n) - * + * * @author [Abhiraj Mandal](https://github.com/DataWorshipper) */ -#include /// for assert -#include -#include +#include /// for assert +#include #include /// for IO operations +#include /** * @namespace bit_manipulation @@ -31,7 +32,7 @@ namespace sliding_window_xor { /** * @brief Computes cumulative XOR of all windows of size k - * + * * @param n Size of the array * @param k Window size * @param x Initial value to generate the array @@ -39,26 +40,20 @@ namespace sliding_window_xor { * @param b Increment in array generation * @param c Modulo in array generation * @returns std::uint64_t The cumulative XOR of all windows of size k - * + * * @details * This function generates the array using the recurrence: * arr[0] = x * arr[i] = (a * arr[i-1] + b) % c - * + * * It maintains a sliding window of size k using two pointers l and r: * - x1 stores the XOR of the current window * - x2 stores the cumulative XOR of all valid windows - * + * * This approach ensures that the algorithm runs in O(n) time. */ -std::uint64_t compute( - std::uint64_t n, - std::uint64_t k, - std::uint64_t x, - std::uint64_t a, - std::uint64_t b, - std::uint64_t c) { - +std::uint64_t compute(std::uint64_t n, std::uint64_t k, std::uint64_t x, + std::uint64_t a, std::uint64_t b, std::uint64_t c) { // Generate the array of n elements std::vector arr(n); arr[0] = x; // First element of the array @@ -109,7 +104,7 @@ static void test() { assert(compute(2, 1, 2, 3, 4, 5) == 2); // Testcase 3: n = 5, k = 2 - assert(compute(5, 2, 1, 1, 1, 100) == 0 ^ 3 ^ 1 ^ 7); + assert(compute(5, 2, 1, 1, 1, 100) == 0 ^ 3 ^ 1 ^ 7); // Testcase 4: n = 3, k = 5, expected = 0 assert(compute(3, 5, 5, 2, 1, 100) == 0); From 813cdca400e49886eb2262904568caa7cd25b36c Mon Sep 17 00:00:00 2001 From: Abhiraj Mandal <152878306+DataWorshipper@users.noreply.github.com> Date: Thu, 2 Oct 2025 19:46:41 +0530 Subject: [PATCH 3/6] Update bit_manipulation/sliding_window_xor.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/sliding_window_xor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp index 2843de51b8..db6ada8194 100644 --- a/bit_manipulation/sliding_window_xor.cpp +++ b/bit_manipulation/sliding_window_xor.cpp @@ -15,7 +15,7 @@ */ #include /// for assert -#include +#include /// for std::uint32_t #include /// for IO operations #include From a6603647d559e1aadb8907d43cf308e43bda7e1c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 2 Oct 2025 14:17:27 +0000 Subject: [PATCH 4/6] clang-format and clang-tidy fixes for 813cdca4 --- bit_manipulation/sliding_window_xor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp index db6ada8194..748bd5bda3 100644 --- a/bit_manipulation/sliding_window_xor.cpp +++ b/bit_manipulation/sliding_window_xor.cpp @@ -14,8 +14,8 @@ * @author [Abhiraj Mandal](https://github.com/DataWorshipper) */ -#include /// for assert -#include /// for std::uint32_t +#include /// for assert +#include /// for std::uint32_t #include /// for IO operations #include From 643f101b7c9701866768b960787d606f500261e7 Mon Sep 17 00:00:00 2001 From: DataWorshipper Date: Fri, 3 Oct 2025 09:45:14 +0530 Subject: [PATCH 5/6] Refactored variable names and added comment lines --- bit_manipulation/sliding_window_xor.cpp | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp index 748bd5bda3..4e72c99c85 100644 --- a/bit_manipulation/sliding_window_xor.cpp +++ b/bit_manipulation/sliding_window_xor.cpp @@ -17,7 +17,7 @@ #include /// for assert #include /// for std::uint32_t #include /// for IO operations -#include +#include /// for std::vector /** * @namespace bit_manipulation @@ -36,15 +36,15 @@ namespace sliding_window_xor { * @param n Size of the array * @param k Window size * @param x Initial value to generate the array - * @param a Multiplier in array generation - * @param b Increment in array generation - * @param c Modulo in array generation + * @param multiplier Multiplier in array generation + * @param increment Increment in array generation + * @param modulo Modulo in array generation * @returns std::uint64_t The cumulative XOR of all windows of size k * * @details * This function generates the array using the recurrence: * arr[0] = x - * arr[i] = (a * arr[i-1] + b) % c + * arr[i] = (multiplier * arr[i-1] + increment) % modulo * * It maintains a sliding window of size k using two pointers l and r: * - x1 stores the XOR of the current window @@ -53,36 +53,36 @@ namespace sliding_window_xor { * This approach ensures that the algorithm runs in O(n) time. */ std::uint64_t compute(std::uint64_t n, std::uint64_t k, std::uint64_t x, - std::uint64_t a, std::uint64_t b, std::uint64_t c) { + std::uint64_t multiplier, std::uint64_t increment, std::uint64_t modulo) { // Generate the array of n elements std::vector arr(n); arr[0] = x; // First element of the array for (std::uint64_t i = 1; i < n; ++i) { - arr[i] = (a * arr[i - 1] + b) % c; // recurrence relation + arr[i] = (multiplier* arr[i - 1] + increment) % modulo; // recurrence relation } std::uint64_t x1 = 0; // XOR of the current window std::uint64_t x2 = 0; // Cumulative XOR of all windows of size k - std::uint64_t l = 0; // Left pointer of sliding window - std::uint64_t r = 0; // Right pointer of sliding window + std::uint64_t left = 0; // Left pointer of sliding window + std::uint64_t right = 0; // Right pointer of sliding window // Slide the window over the array - while (r < n) { - x1 ^= arr[r]; // include current element in window XOR + while (right < n) { + x1 ^= arr[right]; // include current element in window XOR // Shrink window from left if size exceeds k - while (r - l + 1 > k) { - x1 ^= arr[l]; // remove leftmost element from window XOR - ++l; + while (right - left + 1 > k) { + x1 ^= arr[left]; // remove leftmost element from window XOR + ++left; } // If window size equals k, add it to cumulative XOR - if (r - l + 1 == k) { + if (right - left + 1 == k) { x2 ^= x1; } - ++r; // Move right pointer + ++right; // Move right pointer } return x2; // Return cumulative XOR of all windows @@ -122,4 +122,5 @@ static void test() { int main() { test(); // run self-test implementations return 0; + } \ No newline at end of file From 07e65a1a5f269a5bb9d57c9d281e16a5bd09b724 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 3 Oct 2025 04:17:57 +0000 Subject: [PATCH 6/6] clang-format and clang-tidy fixes for 643f101b --- bit_manipulation/sliding_window_xor.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/sliding_window_xor.cpp b/bit_manipulation/sliding_window_xor.cpp index 4e72c99c85..1442ed617c 100644 --- a/bit_manipulation/sliding_window_xor.cpp +++ b/bit_manipulation/sliding_window_xor.cpp @@ -53,19 +53,21 @@ namespace sliding_window_xor { * This approach ensures that the algorithm runs in O(n) time. */ std::uint64_t compute(std::uint64_t n, std::uint64_t k, std::uint64_t x, - std::uint64_t multiplier, std::uint64_t increment, std::uint64_t modulo) { + std::uint64_t multiplier, std::uint64_t increment, + std::uint64_t modulo) { // Generate the array of n elements std::vector arr(n); arr[0] = x; // First element of the array for (std::uint64_t i = 1; i < n; ++i) { - arr[i] = (multiplier* arr[i - 1] + increment) % modulo; // recurrence relation + arr[i] = (multiplier * arr[i - 1] + increment) % + modulo; // recurrence relation } - std::uint64_t x1 = 0; // XOR of the current window - std::uint64_t x2 = 0; // Cumulative XOR of all windows of size k + std::uint64_t x1 = 0; // XOR of the current window + std::uint64_t x2 = 0; // Cumulative XOR of all windows of size k std::uint64_t left = 0; // Left pointer of sliding window - std::uint64_t right = 0; // Right pointer of sliding window + std::uint64_t right = 0; // Right pointer of sliding window // Slide the window over the array while (right < n) { @@ -122,5 +124,4 @@ static void test() { int main() { test(); // run self-test implementations return 0; - } \ No newline at end of file