How to add O(N) and O(M)? [closed] - java

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
Say I have two operations that are O(N), so I represent one as O(N) and the other as, say, O(M). If I added them together, what would it be? There's no telling which one is the greater of the two.
Also, is O(N) + O(M) the same as O(N + M)? If so, why?

If your input is N,M, then yes: O(N) + O(M) = O(N+M).
Why is that?
O(N) + O(M) is bounded by O(N+M):
O(N) + O(M) is a*N + b*M operations [where a, b are constants].
a*N + b*M <= (a+b)*N + (a+b)*M = (a+b)*(N+M) <= c*(N+M) [where c = a + b].
c is constant so c*(N+M) is O(N+M).
O(N+M) is bounded by O(N) +O(M):
O(N+M) = e*(N+M) = e*N + e*M [for some constant e],
which is O(N) + O(M)

Related

How can I make this multiply method return a polynomial that doesn't have repeated exponents? [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 2 years ago.
Improve this question
I need this method to return the simplified polynomial
for example: if the result is 3.00x^4 + 6.00x^2 + 2.00x^3 + 4.00x+x^2 + 2.00 i need it to return it simplified to 3.00x^4 + 2.00x^3 + 7.00x^2 + 4.00x + 2.00
One way to do this is to use an integer array. The coefficient of xn goes in the nth place of the array. And as you add it to the list, you'll add the number to whatever is there. Integer arrays have 0 in every position by default.
This would add polynomials for you.

Which equation represents quadratic time [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 4 years ago.
Improve this question
I believe it is 3n+5n^2+1 but I am not 100 percent sure. If I am wrong can someone explain to me why?
3n + 5n^2 + 1 | applying Big-O notation for the single terms
= O(3n) + O(5n^2) + O(1) | leaving out constant multipliers
= O(n) + O(n^2) + O(1) | taking the one with the maximum power
= O(n^2)
=> quadratic time
So you are right, the first option is the quadratic one.
If it was 3n + 5n^(2 + 1), then it would be qubical due to 5^(2 + 1) = 5^3.

Number of the binary strings without consecutive zeroes of a certain length correlates with Fibonacci numbers. [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 6 years ago.
Improve this question
I'm trying to develop algorithm which would determinate number of binary strings without consecutive zeroes of a certain length. I found solution, which is based on Fibonacci sequence. I don't understand, how the binary string, that ends at the 0 or 1 and not contains repeated 0's depends of Fibonnacci sequence. Can anybody explain it?
For example, for length 3 answer will be 5, because:
000
001
*010
*011
100
*101
*110
*111
*Strings without consecutive zeroes
Let's Z(k) is number of valid binary strings, ending with 0. Denote such string *0
Let's O(k) is number of valid binary strings, ending with 1. Denote such string *1
We can build *0 with length k+1 only with adding 0 to the end of *1, so
Z(k+1) = O(k)
We can build *1 with length k+1 adding 1 both to the end of any *1, and to the end of any *0, so
O(k+1) = O(k) + Z(k)
Consider all valid strings with length (k+2)
F(k+2) =
Z(k+2) + O(k+2) =
O(k+1) + O(k+1) + Z(k+1) =
O(k) + Z(k) + O(k+1) + Z(k+1) =
F(k) + F(k+1)
Do you see Fibonacci-like relation?

Is it possible to insert a number into a sorted array, time: log(n) [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 6 years ago.
Improve this question
I have a sorted array of integers (of size n) and would like to insert a new element into the array, is it possible to do so in O(log(n))? I need to keep an insertion and find computational complexity of O(log(n))).
Right now the only idea I have is to do binary search in order to find the desired index for insertion - this would take O(log(n)), but then I would have to create a new array and copy the entire cells which would take O(n).
EDIT:
It was solved by using an AVL Tree instead, that way any new elements added takes O(log(n)) and finding an element takes O(log(n))
"is it possible to do so in log(n)? " - in short no. From my recollections and experience inserting into an arbitrary position in an array is always O(N), almost by definition. If you want faster performance use something like the TreeMap class.

Big-0 notations Order of Magnitude [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm doing some homework and just want to make sure I am on the right track.
The question is:
Describe the order of magnitude of each of the following functions using Big-O notation.
1) N^2 + 3N = my answer O(N^2)
2) 3N^2 + N = my answer O(N^2)
3) N^5 + 100N^3 + 245 = my answer O(N^5)
4) 3Nlog2N + N^2 – my answer O(N^2)
5) 1 + N + N^2 + N^3 + N^4 = my answer O(N^4)
6) (N * (N – 1)) / 2 - my answer O(N^2)
Am I doing this right? Any suggestions?
The Big-O Notation's Order of magnitude is the one with the highest power (because they are, in most cases, the most computationally expensive function). So, you will have to see, in your formula, which function is most computationally expensive to do.
The first 2 is correct. The other 3, well.... ;)
Update: Question 1, 2, 3 and 6 are correct.

Categories

Resources