using System;
class GFG
{
static int lenOfLongestGP(int []Set, int n)
{
if (n < 2)
{
return n;
}
if (n == 2)
{
return (Set[1] % Set[0] == 0 ? 2 : 1);
}
Array.Sort(Set);
int [,]L = new int[n, n];
int llgp = 1;
for (int i = 0; i < n - 1; ++i)
{
if (Set[n - 1] % Set[i] == 0)
{
L[i, n - 1] = 2;
if (2 > llgp)
llgp = 2;
}
else
{
L[i, n - 1] = 1;
}
}
L[n - 1, n - 1] = 1;
for (int j = n - 2; j >= 1; --j)
{
int i = j - 1, k = j + 1;
while (i >= 0 && k <= n - 1)
{
if (Set[i] * Set[k] < Set[j] * Set[j])
{
++k;
}
else if (Set[i] * Set[k] > Set[j] * Set[j])
{
if (Set[j] % Set[i] == 0)
{
L[i,j] = 2;
if (2 > llgp)
llgp = 2;
}
else
{
L[i,j] = 1;
}
--i;
}
else
{
if (Set[j] % Set[i] == 0)
{
L[i, j] = L[j, k] + 1;
if (L[i, j] > llgp)
{
llgp = L[i, j];
}
}
else
{
L[i, j] = 1;
}
--i;
++k;
}
}
while (i >= 0)
{
if (Set[j] % Set[i] == 0)
{
L[i, j] = 2;
if (2 > llgp)
llgp = 2;
}
else
{
L[i, j] = 1;
}
--i;
}
}
return llgp;
}
public static void Main(String[] args)
{
int []set1 = {1, 3, 9, 27, 81, 243};
int n1 = set1.Length;
Console.Write(lenOfLongestGP(set1, n1) + "\n");
int []set2 = {1, 3, 4, 9, 7, 27};
int n2 = set2.Length;
Console.Write(lenOfLongestGP(set2, n2) + "\n");
int []set3 = {2, 3, 5, 7, 11, 13};
int n3 = set3.Length;
Console.Write(lenOfLongestGP(set3, n3) + "\n");
}
}