Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Time Complexity of Juggling algorithm for array rotation(Suppose 'd' times) is computed as O(n), where n is the size of the array. But for any number of rotation(i.e. for any value of 'd'), the algorithm runs exactly for n times. So, shouldn't the time complexity of the algorithm be "Theta(n)" ? It always loops for n times in any case.If not, can anyone provide a test case where it doesn't run for n times?
It is unclear what you ask, but if we look at https://www.geeksforgeeks.org/array-rotation/ we see that it is described as O(n) time but if we want to rotate zero steps it could be done in O(1) time, so it doesn't always take n times - i.e. Theta(n) would be wrong; but O(n) is correct.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
for(i=1, count=0;i<=n;i=i+k)
for(j=1;j<=k;j++)
count++;
I can't figure out how many times the inner for loop is executing.
I need the answer in terms of n and k
Let's look at the the two loops individually.
The outer loop starts with i=1, and increments it by k in each iteration, until it's greater than n. In other words it can run n/k times, or, to be accurate, floor(n/k) times, since a loop can't run a non-whole number of times.
The inner loop is relatively simpler - it starts with j=1 and increments it by one in each iteration until it's greater than k, for a total of k times.
Put these two together and you'll get floor(n/k)*k.
EDIT:
As pointed out in the comment, this analysis is true if n>=k. If n<k the outer loop will run exactly once. I.e., the total times run would be: max(1, floor(n/k))*k.
Answer: floor(n/k) * k. To check it exactly, I think you can print with print.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm a beginner in C programming so i need some help for my time complexity function.
int function(int n)
{ if (n <= 1)
return n;
int i = random(n-1);
return test(i) + test(n - 1 - i);
}
I don't know how to deal with this problem because of the random function which have O(1) complexity which return randomly numbers.
I don't know how to deal with this problem because of the random function which have O(1) complexity which return randomly numbers.
Well clearly you treat the random(n-1) call itself as a simple (constant time) call. Taken in isolation that is straight forward. The interesting thing is what effect the value returned by the call has on the performance.
Hint: first consider the best-case and worst-case performance for the algorithm.
Hint: for the purposes of analysis, consider a hypothetical version of random which generates a number sequence that is the antithesis of random numbers :-)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How would I do this in 1 loop since I need to loop again and close every second door? Do they want me to loop through the program 100 times? Should I be using pointers ?
Yes, you should loop through the program 100 times if you want to simulate this behavior.
But if you want to know the final condition(Open/Close) then you can have better algorithm:
As every perfect square number only have odd number of factor, if number is perfect square then final condition of door is open otherwise door is close.
If you are interested see perfect square number and Why perfect squares only have odd numbers of factor .
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
how to find all prime numbers between 1 and 10^9 , i know we can use Sieve_of_Eratosthenes for smaller range, but what when range is too large equivalent to 10^6 ?
Up to 10^9 is not really a big deal. First, only look at odd numbers (because there is only one even prime). Second, use a bit array, so you only need 500 million bits or about 62 Megabyte. Even straightforward code should do that in a few seconds at most.
If you go further, you'd do a sieve for numbers from 1 to 10^9, then from 10^9 + 1 to 2 * 10^9 and so on. Above 10^13 it gets interesting and you need to put a bit more effort into it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would I write a function that contains a nested for-loop but is only order(n)? Im not sure if i need to use recursion or not.
If the inner for loop is a constant number of loops rather than a variable number of loops, while the outer loop is a variable number of loops (or vice versa) the time complexity is O(n*C) where C is a constant, which just means O(n) (since big O notation is only concerned with growing factors).