Integers Have Friends (Codeforces 1549D)
Tags: Math, two pointers

British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that "every positive integer was one of his personal friends."It turns out that positive integers can also be friends with each other! You are given an array $$$a$$$ of distinct positive integers. Define a subarray $$$a_i, a_{i+1}, \ldots, a_j$$$ to be a friend group if and only if there exists an integer $$$m \ge 2$$$ such that $$$a_i \bmod m = a_{i+1} \bmod m = \ldots = a_j \bmod m$$$, where $$$x \bmod y$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$.Your friend Gregor wants to know the size of the largest friend group in $$$a$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Each test case begins with a line containing the integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$), the size of the array $$$a$$$.The next line contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le {10}^{18}$$$), representing the contents of the array $$$a$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.It is guaranteed that the sum of $$$n$$$ over all test cases is less than $$$2\cdot 10^5$$$.

Output: Your output should consist of $$$t$$$ lines. Each line should consist of a single integer, the size of the largest friend group in $$$a$$$.

Note: In the first test case, the array is $$$[1,5,2,4,6]$$$. The largest friend group is $$$[2,4,6]$$$, since all those numbers are congruent to $$$0$$$ modulo $$$2$$$, so $$$m=2$$$.In the second test case, the array is $$$[8,2,5,10]$$$. The largest friend group is $$$[8,2,5]$$$, since all those numbers are congruent to $$$2$$$ modulo $$$3$$$, so $$$m=3$$$.In the third case, the largest friend group is $$$[1000,2000]$$$. There are clearly many possible values of $$$m$$$ that work.

Sample Inputs:
4
5
1 5 2 4 6
4
8 2 5 10
2
1000 2000
8
465 55 3 54 234 12 45 78

Sample Outputs:
3
3
2
6

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()
import math

def solve():
    (n, arr) = (readInt(), readList())
    d = [abs(arr[i + 1] - arr[i]) for i in range(n - 1)]
    ans = f = g = 0
    for i in range(n - 1):
        g = math.gcd(g, d[i])
        if d[i] == 1:
            (f, g) = (i + 1, 0)
        elif g > 1:
            ans = max(ans, i - f + 1)
        else:
            (g, p) = (d[i], f)
            for j in range(i, p - 1, -1):
                if math.gcd(g, d[j]) == 1:
                    f = j + 1
                    break
                g = math.gcd(g, d[j])
            if g > 1:
                ans = max(ans, i - f + 1)
    return ans + 1
for _ in range(int(input())):
    print(solve())

Solution (Python):
import sys
from math import gcd
input = sys.stdin.readline
t = int(input())
while t > 0:
    n = int(input())
    arr = [*map(int, input().split())]
    a = [0] + arr
    g = 0
    p = 0
    c = 1
    for i in range(2, n + 1):
        g = gcd(g, abs(a[i] - a[i - 1]))
        if g > 1:
            c = max(c, i - p)
        else:
            g = 0
            p = i
            while g != 1:
                sg = g
                g = gcd(g, abs(a[p] - a[p - 1]))
                p -= 1
            g = sg
    print(c)
    t -= 1

Solution (Python):
from sys import stdin, stdout
from collections import defaultdict
import math

def get_list():
    return list(map(int, stdin.readline().strip().split()))

def get_int():
    return int(stdin.readline())

def get_ints():
    return map(int, stdin.readline().strip().split())

def get_string():
    return stdin.readline()

def printn(n):
    stdout.write(str(n) + '\n')

def solve():
    n = get_int()
    a = get_list()
    if n == 1:
        print(1)
        return
    b = [0] * (n - 1)
    for i in range(n - 1):
        b[i] = abs(a[i] - a[i + 1])
    mp = defaultdict(lambda : 0)
    ans = 0
    if b[0] != 1:
        mp[b[0]] = 1
        ans = 1
    for i in range(1, n - 1):
        curr = defaultdict(lambda : 0)
        if b[i] != 1:
            curr[b[i]] = 1
            ans = max(ans, 1)
        for x in mp:
            g = math.gcd(x, b[i])
            if g != 1:
                curr[g] = max(curr[g], 1 + mp[x])
                ans = max(ans, curr[g])
        mp = curr
    printn(ans + 1)
if __name__ == '__main__':
    t = get_int()
    while t:
        t -= 1
        solve()