You are given an array a of n non-negative integers. Let's define f(a, x) = [a1 mod x, a2 mod x, ..., an mod x] for some positive integer x. Find the biggest x, such that f(a, x) is a palindrome.

Here, a mod x is the remainder of the integer division of a by x.

An array is a palindrome if it reads the same backward as forward. More formally, an array a of length n is a palindrome if for every i (1 ≤ in), ai = an - i + 1.

Input The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.

The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).

The second line of each test case contains n integers ai (0 ≤ ai ≤ 10^9).

It's guaranteed that the sum of all n does not exceed 10^5.

Output For each test case output the biggest x, such that f(a, x) is a palindrome. If x can be infinitely large, output 0 instead.

Example

inputCopy
4
2
1 2
8
3 0 1 2 0 3 2 1
1
0
3
100 1 1000000000
outputCopy
1
2
0
999999900

Solution The problem can be solved using binary search. We can search for the biggest x that satisfies the condition. The condition is that f(a, x) is a palindrome.

To check if f(a, x) is a palindrome, we can sort it and then check if the first half of the array is equal to the second half. This can be done in linear time using two pointers.

Code (Python)

def is_palindrome(a, x):
    f = [ai % x for ai in a]
    f.sort()
    n = len(f)
    for i in range(n // 2):
        if f[i] != f[n - i - 1]:
            return False
    return True

def find_max_palindrome_modulus(a):
    n = len(a)
    left = 1
    right = max(a) + 1
    while left < right:
        mid = (left + right) // 2
        if is_palindrome(a, mid):
            left = mid + 1
        else:
            right = mid
    if left == 1:
        return 0
    else:
        return left - 1

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    print(find_max_palindrome_modulus(a))

Time Complexity The time complexity of the solution is O(n log n log MAX), where MAX is the maximum value in the array.

  • Binary search takes O(log MAX) time.
  • Sorting the array takes O(n log n) time.
  • Checking if the array is a palindrome takes O(n) time.

Explanation

  1. is_palindrome(a, x): This function takes the array a and the value x as input. It calculates the array f(a, x), sorts it, and then checks if the sorted array is a palindrome using two pointers. If it's a palindrome, it returns True, otherwise False.
  2. find_max_palindrome_modulus(a): This function takes the array a as input. It uses binary search to find the biggest x such that f(a, x) is a palindrome. It starts with the search space from 1 to the maximum value in the array. If the left and right pointers are equal to 1, it means that the x can be infinitely large, so it returns 0. Otherwise, it returns left - 1, which is the biggest x that satisfies the condition.
  3. Main Part: The main part of the code reads the input, calls the find_max_palindrome_modulus function, and prints the output.

This code provides a clear and efficient solution for finding the biggest x such that f(a, x) is a palindrome. It utilizes binary search to optimize the search process and employs two pointers for palindrome checking. The time complexity is optimized, resulting in a fast and effective algorithm.

Find the Biggest Palindrome Modulus

原文地址: https://www.cveoy.top/t/topic/nQmX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录