Skip to content

Commit 643f101

Browse files
Refactored variable names and added comment lines
1 parent a660364 commit 643f101

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

bit_manipulation/sliding_window_xor.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <cassert> /// for assert
1818
#include <cstdint> /// for std::uint32_t
1919
#include <iostream> /// for IO operations
20-
#include <vector>
20+
#include <vector> /// for std::vector
2121

2222
/**
2323
* @namespace bit_manipulation
@@ -36,15 +36,15 @@ namespace sliding_window_xor {
3636
* @param n Size of the array
3737
* @param k Window size
3838
* @param x Initial value to generate the array
39-
* @param a Multiplier in array generation
40-
* @param b Increment in array generation
41-
* @param c Modulo in array generation
39+
* @param multiplier Multiplier in array generation
40+
* @param increment Increment in array generation
41+
* @param modulo Modulo in array generation
4242
* @returns std::uint64_t The cumulative XOR of all windows of size k
4343
*
4444
* @details
4545
* This function generates the array using the recurrence:
4646
* arr[0] = x
47-
* arr[i] = (a * arr[i-1] + b) % c
47+
* arr[i] = (multiplier * arr[i-1] + increment) % modulo
4848
*
4949
* It maintains a sliding window of size k using two pointers l and r:
5050
* - x1 stores the XOR of the current window
@@ -53,36 +53,36 @@ namespace sliding_window_xor {
5353
* This approach ensures that the algorithm runs in O(n) time.
5454
*/
5555
std::uint64_t compute(std::uint64_t n, std::uint64_t k, std::uint64_t x,
56-
std::uint64_t a, std::uint64_t b, std::uint64_t c) {
56+
std::uint64_t multiplier, std::uint64_t increment, std::uint64_t modulo) {
5757
// Generate the array of n elements
5858
std::vector<std::uint64_t> arr(n);
5959
arr[0] = x; // First element of the array
6060

6161
for (std::uint64_t i = 1; i < n; ++i) {
62-
arr[i] = (a * arr[i - 1] + b) % c; // recurrence relation
62+
arr[i] = (multiplier* arr[i - 1] + increment) % modulo; // recurrence relation
6363
}
6464

6565
std::uint64_t x1 = 0; // XOR of the current window
6666
std::uint64_t x2 = 0; // Cumulative XOR of all windows of size k
67-
std::uint64_t l = 0; // Left pointer of sliding window
68-
std::uint64_t r = 0; // Right pointer of sliding window
67+
std::uint64_t left = 0; // Left pointer of sliding window
68+
std::uint64_t right = 0; // Right pointer of sliding window
6969

7070
// Slide the window over the array
71-
while (r < n) {
72-
x1 ^= arr[r]; // include current element in window XOR
71+
while (right < n) {
72+
x1 ^= arr[right]; // include current element in window XOR
7373

7474
// Shrink window from left if size exceeds k
75-
while (r - l + 1 > k) {
76-
x1 ^= arr[l]; // remove leftmost element from window XOR
77-
++l;
75+
while (right - left + 1 > k) {
76+
x1 ^= arr[left]; // remove leftmost element from window XOR
77+
++left;
7878
}
7979

8080
// If window size equals k, add it to cumulative XOR
81-
if (r - l + 1 == k) {
81+
if (right - left + 1 == k) {
8282
x2 ^= x1;
8383
}
8484

85-
++r; // Move right pointer
85+
++right; // Move right pointer
8686
}
8787

8888
return x2; // Return cumulative XOR of all windows
@@ -122,4 +122,5 @@ static void test() {
122122
int main() {
123123
test(); // run self-test implementations
124124
return 0;
125+
125126
}

0 commit comments

Comments
 (0)