CF BUDDY
← Problems·

1980D · GCD-sequence

1400 · greedy, implementation, math

Problem: GCD (Greatest Common Divisor) of two integers xx and yy is the maximum integer zz by which both xx and yy are divisible. For example, GCD(36,48)=12GCD(36, 48) = 12, GCD(5,10)=5GCD(5, 10) = 5, and GCD(7,11)=1GCD(7,11) = 1.

Kristina has an array aa consisting of exactly nn positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array bb, called GCD-sequence.

So, the elements of the GCD-sequence bb will be calculated using the formula bi=GCD(ai,ai+1)b_i = GCD(a_i, a_{i + 1}) for 1in11 \le i \le n - 1.

Determine whether it is possible to remove exactly one number from the array aa so that the GCD sequence bb is non-decreasing (i.e., bibi+1b_i \le b_{i+1} is always true).

For example, let Khristina had an array aa = [20,6,12,3,48,3620, 6, 12, 3, 48, 36]. If she removes a4=3a_4 = 3 from it and counts the GCD-sequence of bb, she gets:

  • b1=GCD(20,6)=2b_1 = GCD(20, 6) = 2
  • b2=GCD(6,12)=6b_2 = GCD(6, 12) = 6
  • b3=GCD(12,48)=12b_3 = GCD(12, 48) = 12
  • b4=GCD(48,36)=12b_4 = GCD(48, 36) = 12

Input Format: The first line of input data contains a single number tt (1t1041 \le t \le 10^4) — he number of test cases in the test.

This is followed by the descriptions of the test cases.

The first line of each test case contains a single integer nn (3n21053 \le n \le 2 \cdot 10^5) — the number of elements in the array aa.

The second line of each test case contains exactly nn integers aia_i (1ai1091 \le a_i \le 10^9) — the elements of array aa.

It is guaranteed that the sum of nn over all test case does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single line:

  • "YES" if you can remove exactly one number from the array aa so that the GCD-sequence of bb is non-decreasing;
  • "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).

Note: The first test case is explained in the problem statement.

Sample Cases

Case 1

Input

12
6
20 6 12 3 48 36
4
12 6 3 4
3
10 12 3
5
32 16 8 4 2
5
100 50 2 10 20
4
2 4 8 1
10
7 4 6 2 4 5 1 4 2 8
7
5 9 6 8 5 9 2
6
11 14 8 12 9 3
9
5 7 3 10 6 3 12 6 3
3
4 2 4
8
1 6 11 12 6 12 3 6

Output

YES
NO
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback