Find the Biggest Palindrome Modulo
Problem Statement
You are given an array 'a' containing 'n' non-negative integers. Define 'f(a, x)' as an array formed by taking the modulo of each element in 'a' by a positive integer 'x'. Formally, 'f(a, x) = [a1 mod x, a2 mod x, ..., an mod x]'.
Your task is to find the biggest possible value of 'x' such that 'f(a, x)' is a palindrome.
Example:
Input:
4
2
1 2
8
3 0 1 2 0 3 2 1
1
0
3
100 1 1000000000
Output:
1
2
0
999999900
Explanation:
- Test case 1: For 'x = 1', 'f(a, 1) = [1, 0]'. This is a palindrome, and there is no larger value of 'x' that would also create a palindrome. Therefore, the answer is '1'.
- Test case 2: For 'x = 2', 'f(a, 2) = [1, 0, 1, 0, 0, 1, 0, 1]'. This is a palindrome, and there is no larger value of 'x' that would also create a palindrome. Therefore, the answer is '2'.
- Test case 3: For any value of 'x' greater than 0, 'f(a, x) = [0]'. This is always a palindrome, so 'x' can be infinitely large. Therefore, the answer is '0'.
- Test case 4: For 'x = 999999900', 'f(a, x) = [100, 1, 100]' which is a palindrome. There's no larger 'x' that produces a palindrome, making the answer '999999900'.
Solution
The solution involves iterating over possible values of 'x' and checking if the resulting 'f(a, x)' is a palindrome.
-
Preprocessing: Create a boolean array 'vis' to store whether a number appears in the input array 'a'. This helps efficiently check if the modulo operation for a specific 'x' results in elements that are present in the original array.
-
Iterate over 'x': Start from 'x = 2' and go up to a reasonable limit (100000 in this case). For each 'x', check if 'f(a, x)' is a palindrome.
-
Palindrome Check: For every 'x', check if the modulo values for 'j' and '(i - j)' are both present or both absent in the input array 'a' using the 'vis' array. If they are not, then 'f(a, x)' is not a palindrome.
-
Update Answer: If 'f(a, x)' is a palindrome, update the answer 'ans' with the current value of 'x'.
Code (C++)
#include <bits/stdc++.h>
using namespace std;
int t, n, a[100010], vis[100010];
int main() {
cin >> t;
while (t--) {
cin >> n;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
cin >> a[i];
vis[a[i]] = 1;
}
int ans = 0;
for (int i = 2; i <= 100000; i++) { // Iterate over possible values of 'x'
bool flag = 1;
for (int j = 0; j <= i / 2; j++) { // Check for palindrome
if (vis[j % i] != vis[(i - j) % i]) {
flag = 0;
break;
}
}
if (flag) ans = i; // Update the answer if a palindrome is found
}
cout << ans << endl;
}
return 0;
}
Key Points:
- Time Complexity: The code runs in O(n * max_x), where 'n' is the size of the array and 'max_x' is the upper limit for 'x'.
- Space Complexity: The code uses O(max_x) extra space for the 'vis' array.
Optimization:
- Efficient modulo calculation: The code calculates the modulo value in the palindrome check loop, avoiding redundant modulo calculations.
- Early termination: The palindrome check loop terminates early if a mismatch is found, saving unnecessary iterations.
This solution provides a concise and efficient way to solve the given problem.
原文地址: https://www.cveoy.top/t/topic/nQmy 著作权归作者所有。请勿转载和采集!