private static int f(int[] a, int low, int high)
{
int res = 0;
for (int i = low; i <= high; i++)
res += a[i];
return res;
}
/**
*
* @return the size of the largest gap in the array in which the combined values contained in the indexes are divisible by 3
*
*/
public static int what(int[] a)
{
int temp = 0;
for (int i = 0; i < a.length; i++)
{
for (int j = i; j < a.length; j++)
{
int c = f(a, i, j);
if (c % 3 == 0)
{
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
我需要计算what
的算法复杂度。我认为它可能是n^2
,因为它有两个循环,但它也可能是n^3
,因为它使用f
,它有另一个循环。如何确定其算法复杂度?