hey, can anyone help me with these comlexity problems? I tried with myself but still want some clarifications..cheer,here is the code:
(a) public void printPow2(int[] values)
{
for (int i = 1; i < values.length; i *= 2)
System.out.println(values[i]);
}
(b) public void maxDifference(int[] values)
{
int max = 0;
for (int i = 0; i < values.length; i++) {
for (int j = i+1; j < values.length; j++) {
int diff = Math.abs(values[j]-values[i]);
if (max < diff)
max = diff;
}
}
return max;
}
(c) public int fac(int n)
{
if (n == 0)
return 0;
else
return n*fac(n-1);
}
(d) public int silly(int n)
{
int tally = 0;
for (int i=0; i < n; i++) {
for (int j=n; j < n; j--) {
tally += 1;
}
}
return tally;
}
so the question is to find the running time comlexity of each question,with input size n,and it is for the worst case.
so my solution is (a)O(n),since the for loop tests n/2 times(or is it n/2 +1 times?need clarification), i*=2 excutes n times, so total of n/2+n=O(n).(does the system.out count by O(1)?)
(b)O(n^2) since only the nested loop dominant
(c)need help with this
(d)O(n^2) as for (b)
cheers in adevance,its kinda urgent please help..