Return value of q = O(n log log n) and Time Complexity = O( n log n).
Explanation: Outer for loop will run for 'n' times.
For every value of i, the inner loop for (j=n; j>1; j=j/2) ++p;
will run for (log n) times.
Now the value of 'p' will be approximately log n.
So the other for loop for (k=1; k<p; k=k*2) ++q;
will run for (log log n) times.
Note: for (k=1; k<p; k=k*2) ++q;
is not nested loop of for (j=n; j>1; j=j/2) ++p;.
As the outer loop is running for almost n times.
Time Complexity = O( n log n + n log log n) = O(n log n).
But the return value of q will be (n log log n).