The what
method has a time complexity of O(n^2) and it uses the method f
with time complexity of O(n). I need to calculate the overall time complexity of method what
. Do I also take in account the time complexity of method f
, so overall the time complexity of method what
would be O(n^2 * n = n^3), or does each method take care of its own time complexity, so in this case the method what
will stay at time complexity O(n^2)?
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;
}
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%2 == 0)
{
if (j-i+1 > temp)
temp = j-i+1;
}
}
}
return temp;
}