recategorized by
544 views
3 votes
3 votes

Consider following program and determine the value of p after execution of program:-

main()
{
    p = 1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j=5*j)
        {
            p = p + n;
        }
    }
}

I think p's final value should be p = 1+n2log5n.

recategorized by

2 Answers

0 votes
0 votes

Time complexity will be O(n log5n ) because.

1. The inner loop is independent of the outer loop. For each value of i  It will run log5n each time.

so, for n times it will be = O(n log5n ).

for(i=1;i<=n;i++) =================================== O(n)
    {
        for(j=1;j<=n;j=5*j) { =======================log
5n p = p + n; } }

Because inner loop doesn't depend on the on the outer loop so, directly multiply here O(n log5n ).

edited by anonymous

Related questions

0 votes
0 votes
1 answer
1
Shubhanshu asked Sep 24, 2017
770 views
Consider the following codeWhat is the output printed by the above code in terms of n?How it is coming b??
2 votes
2 votes
1 answer
2
Nishikant kumar asked Nov 16, 2015
1,027 views
P(X:integer,Y:integer) { X = 6; A = 8; return ( X + Y ) }if the function P were invoked by the following program fragment. k = 1; L = 1; Z = (K, L);Then the value of Z wo...
4 votes
4 votes
2 answers
3
1 votes
1 votes
2 answers
4