1,926 views
1 votes
1 votes

void fun(int n, int k) 

{  

for (int i=1; i<=n; i++)   

{      

int p = pow(i, k);  

 for (int j=1; j<=p; j++)  

    {           // Some O(1) work    

  }    

}

}

2 Answers

3 votes
3 votes

We need to calculate upper bound of this series summation.S =  $1^{k} + 2^{k} + 3^{k} + ------ + n^{k}$.

for k = 1 S = $\large \frac{n*(n+1)}{2} = O\left ( n^{2} \right )$ 

for k = 2 S = $\large \frac{n*(n+1)(2*n+1)}{6} = O\left ( n^{3} \right )$

for k = 3 S = $\large \left ( \frac{n*(n+1)}{2} \right )^{2} = O\left ( n^{4} \right )$

--------------------------------------------------------

In this question ans should be  = $\large O\left ( n^{k+1} \right )$

1 votes
1 votes

Here inner loop is running for p times which is equal to ik.

So, Total time complexity for all i ( 1<=i <=n). will be,

T(n)= 1k + 2k + 3k + 4k + ...... + nk     

      = (n+1)k

      = O((n+1)k

https://arxiv.org/pdf/1011.2154v1.pdf

edited by

Related questions

2 votes
2 votes
2 answers
1
Amar Vashishth asked Aug 2, 2015
2,446 views
int fun(int n) { int count=0; for (int i= n; i 0; i/=2) for(int j=0; j< i; j++) count+= 1; return count; }
0 votes
0 votes
2 answers
3
radha gogia asked Jul 7, 2018
1,587 views
foo(int n) { for(int i=0 ; i<n ;i++) for(int j=i ; j<=i*i ;j++) if(j%i==0) { for(int k=0;k<j;k++) printf("hii"); } } How to proceed here for analyzing the time complexity...
3 votes
3 votes
1 answer
4
Sanjay Sharma asked Feb 20, 2018
1,131 views
An array A is of length n has log( n) distinct numbers.What is the time complexity of sorting A by best comparison based algorithm ?