0 votes 0 votes For p in1..N*N loop For q in 1..p loop Sum=Sum-1 What is the running time for sum statement?I think it will be 1+2+3+4..N^2.But how to slove this? Algorithms algorithms time-complexity + – Atul Verma12 asked Sep 17, 2016 • retagged Jun 20, 2022 by makhdoom ghaya Atul Verma12 313 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes your code looks like for (p=1 to n*n) for(q=1 to p) sum=sum-1 time complexity of the above snippet will be equal to number of times "sum=sum-1" runs. p q 1 1 2 2 3 3 ...... ..... n*n n*n $T\left ( n \right )=1+2+3+...n^{2}=\left ( n^{2}*\left ( n^{2}+1 \right ) \right )/2$=O$\left ( n^{4} \right )$ sourav. answered Sep 17, 2016 • edited Sep 19, 2016 by sourav. sourav. comment Share Follow See all 2 Comments See all 2 2 Comments reply Atul Verma12 commented Sep 18, 2016 reply Follow Share T(n)=1+2+3+...n2=(n2∗(n2+1))/2T(n)=1+2+3+...n2=(n2∗(n2+1))/2=O(n^4) not n^2 1 votes 1 votes sourav. commented Sep 18, 2016 reply Follow Share updated !thankx 0 votes 0 votes Please log in or register to add a comment.