Why is the algorithm complexity O(N*log(N+M))? [closed] - algorithm

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.

Related

How to find the largest, 2nd, 4th, 8th...2^logn th largest element in an array, use O(n) algorithm [closed]

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!

Is O(nk(log(k))) algorithm same as O(n(log(k))) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was asked to give an algorithm that was supposed to be O(n(log(k)))
k is the number of arrays and n is the total number of elements in all of these. I had to sort the arrays.
Minus the details I came up with an algorithm that does the job for in klog(k) times the total number of elements. i.e. O(nk(log(k)))
Also in this case k is much smaller than n so it wont be n^2(logn) (in case k and n were almost same)right?
Well, no, it's not the same. If k is a variable (as opposed to a constant) in the complexity expression then O(nk(log(k))) > O(n(log(k))).
That is because there is no constant C such that Cn(log(k)) > kn(log(k)) for every n, k.
The way you describe the question both k and n are input parameters. If that is the case then the answer to your question is
'No, O(n*k *log(k)) is not the same as O(n*log(k))'.
It is not that hard to see that the first one grows faster than the second one, but it is even more obvious if you fix the value of n. Consider n begin a constant say 1. Than it is more obvious that O(k*log(k)) is not the same as O(log(k)).

Do we really have case in this algorithm [closed]

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 9 years ago.
Improve this question
I am having trouble for solving the running time of the following algorithm
Now first my question, is the case really important here(I can not come up with 2 different inputs of the same size that are different from each other) ?
Second, I think this algorithm runs in O(n^2). Am I right?
The comment you wrote in #OBu's answer is about only a quarter right:
1*n + 2*(n-1) + 3*(n-2) + ... +n*1
That equals to:
Sum(i=1..n, i*(n-i+1)) = n*Sum(i) - Sum(i*i) + n = n*[n(n+1)/2] - [n(n+1)(2n+1)/6] + n
If you want, feel free to compute the exact formula, but the overall complexity is O(n^3).
As a rule of thumb (more like a back-of-the-envelope computation trick I've picked up... just to give you a quick idea): if you are unsure about algorithms with multiple for's (with different lengths, but all in relation with n, as you have above) try to compute how many operations are performed around the middle of the algorithm (n/2). This usually gives you an idea on how the running time complexity for the whole thing might looks like - you are basically computing the largest element in the sum, so the overall complexity is always >= than the thing you compute (in most cases it's the same though).
Just to give you some hints:
How many loops do you have and how are they nested?
How often is each loop running (start solving this from the outer to the inner loop)
If in doubt, try it with n=4 or 5 and calculate each step. After this, you'll have a good feeling for what's going on.

Maximum no of overlaps of all time intervals [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a set of time intervals , how to find the find the maximum no of overlaps . Is there any algorithm which solves the given problem with time complexity O(n log n ) or O(n)??
example : (6:00-9:30),(9:00-12:30),(10:00-10:30), (12:00-14:30), (11:00-13:30).The answer is 3
Sort the values using quick sort -- O(nlogn) time.
6:00(+)
9:30(-)
9:00(+)
12:30(-)
10:00(+)
10:30(-)
12:14:30(Dude wut?) --> Im going to assume you meant 12:00(+) ,14:30(-)
11:00(+)
13:30(-)
Becomes
6:00(+)
9:00(+)
9:30(-)
10:00(+)
10:30(-)
11:00(+)
12:00(+)
12:30(-)
13:30(-)
14:30(-)
Iterate through the list incrementing for every plus and decrementing for every minus, record the max value found. This takes O(n) time
Total time O(nlogn + n) = O(nlogn)

Algorithmic classification [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to classify the following problem:
I have N empty boxes (ni is the volume of the i-th box, 1 <= i <= N) and M divisible items (mj is the volume of j-th item j, 1 <= j <= M). The total volume of all boxes is exactly equal to the total volume of all items. I need to find a distribution of items among boxes which minimizes the number of item divisions.
I suppose this problem is NP-complete, and is some kind of set coverage problem, but I can't find appropriate variation of it.
The special case N=2 and n_1 = n_2 is exactly the Subset Sum problem
http://en.wikipedia.org/wiki/Subset_sum_problem
since the optimum value of the problem formulated above is 0 if and only if
the instance (viewed as an instance of Subset Sum) has a solution. Hence, the presented problem is indeed NP-hard.

Resources