17
17
#include < cassert> // / for assert
18
18
#include < cstdint> // / for std::uint32_t
19
19
#include < iostream> // / for IO operations
20
- #include < vector>
20
+ #include < vector> // / for std::vector
21
21
22
22
/* *
23
23
* @namespace bit_manipulation
@@ -36,15 +36,15 @@ namespace sliding_window_xor {
36
36
* @param n Size of the array
37
37
* @param k Window size
38
38
* @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
42
42
* @returns std::uint64_t The cumulative XOR of all windows of size k
43
43
*
44
44
* @details
45
45
* This function generates the array using the recurrence:
46
46
* arr[0] = x
47
- * arr[i] = (a * arr[i-1] + b ) % c
47
+ * arr[i] = (multiplier * arr[i-1] + increment ) % modulo
48
48
*
49
49
* It maintains a sliding window of size k using two pointers l and r:
50
50
* - x1 stores the XOR of the current window
@@ -53,36 +53,36 @@ namespace sliding_window_xor {
53
53
* This approach ensures that the algorithm runs in O(n) time.
54
54
*/
55
55
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 ) {
57
57
// Generate the array of n elements
58
58
std::vector<std::uint64_t > arr (n);
59
59
arr[0 ] = x; // First element of the array
60
60
61
61
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
63
63
}
64
64
65
65
std::uint64_t x1 = 0 ; // XOR of the current window
66
66
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
69
69
70
70
// 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
73
73
74
74
// 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 ;
78
78
}
79
79
80
80
// If window size equals k, add it to cumulative XOR
81
- if (r - l + 1 == k) {
81
+ if (right - left + 1 == k) {
82
82
x2 ^= x1;
83
83
}
84
84
85
- ++r ; // Move right pointer
85
+ ++right ; // Move right pointer
86
86
}
87
87
88
88
return x2; // Return cumulative XOR of all windows
@@ -122,4 +122,5 @@ static void test() {
122
122
int main () {
123
123
test (); // run self-test implementations
124
124
return 0 ;
125
+
125
126
}
0 commit comments