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
What could be the best or easiest way for analyzing/solving time complexity of a program? Both for iterative and recursive methods. Thanks for your help
Count how many times your basic operation will be executed.
For example:
int a = 0;
for (int i = 0; i < 5; i++){
a += 1;
}
your basic operation is a+=1. so how many times will it be executed? for the example, 5. Now instead of 5, we use n. so how many times will your basic operation execute? n times. then you can say the time complexity is O(n). This also goes for the recursive methods.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Consider the following code (Github link)
The variable min is is at most M and the variable max is at most M*N
We run a binary search on the interval [min, max]. Each iteration we call to divisionSolvable which is O(N) so IMHO the overall complexity time is O(N*log(NM)).
Can you please explain why it isn't so?
Not looking at the link, note that O(log(NM)) = O(log(N+M)).
Indeed, for N>=2 and M>=2, we have:
log (N+M) < log(NM) = log(N) + log(M) < log(N+M) + log(N+M) = 2 log(N+M).
Complete that with the definition of O() which drops the constant.
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 6 years ago.
Improve this question
Number of independent trials is N, probability of success is p. I want to calculate
Probability of m consecutive successes.
Probability of m or more consecutive successes.
The numbers are very large, so the algorithm should be highly optimized.
N = 877646440
m = 79279,
p = 6204/6205 (or 0.999838839645447....)
I seem to have the answer on mathematical SE where I originally started this question. https://math.stackexchange.com/questions/1888887/easily-calculable-minimum-probability-for-m-or-more-consecutive-outcomes/1889372#1889372
I will implement that solution and update the questions.
Edit: I have gotten the answer on the mathematical SE question and implemented the solution.
Thanks
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
In the interval covering problem, we are given n intervals
[s1,t1), [s2,t2), ···, [sn,tn)
such that
S i∈[n][si,ti) = [0,T).
The goal of the problem is to return a smallest-size set
S ⊆ [n]
such that
S i∈S[si,ti) = [0,T).
Design a greedy algorithm for this problem.
A greedy algorithm could be devised as follows. As long as there is a point p in [0,T) which is not contained in one of the already selected intervals, select an interval [s_i,t_i) , which must exist, since the union of all [s_i,t_i) is [0,T) as stated in the requirements. As the set of intervals [s_i,t_i) is finite, this procedure must terminate.
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
Is there an O(n)-time algorithm to find the largest, second-largest, fourth-largest, ..., 2kth-largest, etc. elements in an array?
Since this looks like a homework question, I won't give a full answer. However, here are a few helpful hints:
Do you know how to find the kth largest element in an array in time O(n)? If not, you probably need to look this up before you're going to make any progress.
1 + 2 + 4 + 8 + ... + 2log n = 2n - 1, which is O(n).
Use the algorithm from part (1) on intelligently-chosen arrays. The summation from (2) will help you prove that you have the runtime bound you need.
Hope this helps!
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
I need help with the following problem:
for i <- 5 to m do P(i)
where P(i) is executed (m-i) times provided m >= 3
I realize that this is the summation
But I'm not sure exactly how to calculate the run time from this. Any suggestions?
If you count complexity of P(i) as constant, then you have just loop m - 5 times - it will give you complexity O(m)