retagged by
420 views
0 votes
0 votes
Hi everyone.
I’ve gone through this two issues and solved them, please correct me if something looks wrong.
thanks.

sum1=0

for(k=1; k<=n; k*2)

for(j=1; j <= n; j++)

sum1++;

Sigma from k=1 to n/2 sigma from j=1 to n (1) = Sigma from k=1 to n/2 (n)= n/2 * n = n^2/2

 

sum2=0;

for(k=1;k<=n;k*=2)

for(j=1; j<=k; j++)

sum2++;

 

(n/2(n/2-1))/2=((n^2/4)-(n/2))/2=(n^2-2n)/n
retagged by

1 Answer

0 votes
0 votes

ans 1.

sum1=0;

for(k=1; k<=n; k*2).....//it executes log n + 1 times

for(j=1; j <= n; j++)....// it executes n times for each iteration.

sum1++;

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

n k ( executes log n + 1 times) j (executes n times for each iteration)
1 1 1
2 1,2 i.e. 2 times (1,2) , (1,2)
4 1,2,4 i.e. 3 times (1,2,3,4) , (1,2,3,4) ,(1,2,3,4)
8 1,2,4,8 i.e. 4 times (1,2,3,4,5,6,7,8), (1,2,3,4,5,6,7,8), (1,2,3,4,5,6,7,8), (1,2,3,4,5,6,7,8)
... ............................ ......................
n logn + 1 times n times for each iteration

T.C. = O(no. of times outer loop executes * no. of times inner loop executes for each outer loop iteration)

       = O((log n + 1) *n)

       = O(n log n)

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

 

ans 2.

sum2=0;

for(k=1;k<=n;k*=2)....// it executes log n + 1 times

for(j=1; j<=k; j++)...// it executes k times.

sum2++;
n k ( executes log n + 1 times) j (executes k times for each iteration)
1 1 1 i.e. 1 time
2 1,2 i.e. 2 times (1 ), (1,2 ) i.e 3 times
4 1,2,4 i.e. 3 times (1) , (1,2) , (1,2,3) i.e. 6 times
8 1,2,4,8 i.e. 4 times (1) , (1,2) , (1,2,3) , (1,2,3,4) ... 10 times
... ............................ ......................
n logn + 1 times

  (1) , (1,2) , (1,2,3) , (1,2,3,4) ... (1,2,3,4,..k)

i.e. k(k+1)/2 times

T.C. =  O$\left ( \frac{k*(k+1)}{2} \right )$ 

       = O$\left ( \frac{(log n + 1)*( log n + 1 +1)}{2} \right )$ 

       = O( log n * log n )

edited by

Related questions

0 votes
0 votes
1 answer
1
miller asked Feb 3, 2019
460 views
Hi everyone!I've been recently asked by one of my friends to prove an equation but still, I'm confused how to get it started tho.log(n!) = Ω(nlog(n))Does anyone know ho...
11 votes
11 votes
2 answers
3
sourav. asked Sep 9, 2017
4,531 views
My Question $\{\langle M \rangle \mid M$ is a TM and there exist an input whose length is less than 100, on which $M$ halts$\}$I have to check that it is Turing Recogniza...
–3 votes
–3 votes
0 answers
4
rahuldb asked Nov 30, 2016
530 views
Scan convert a circle with radius 9.5 and centerd at origin using Mid Point Circle AlgorithmPlease show working.