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
What will be the Big Oh of these portion of codes:
int sum = 0;
for(int i = 1; i < N; i *= 2)
for(int j =0; j <i; j++)
sum++;
And
int sum = 0;
for(int i = 0; i < N; i *= 2)
for(int j =0; j <i; j++)
sum++;
My Attempt:
According to me both have time complexity equal to O(n^2), because here we will multiply n with n which is equal to n^2. Am I correct? Or doing some mistake?
For the first portion of code:
1st loop will go from 1 to n with variable i going as
1, 2, 4, 8, 16.... n
and in the second loop j goes from 0 to i so the time complexity will be
O(1 + 2 + 4 + 8 + 16.... n) = O(2n - 1) = O(n)
and as for the second portion of code
i starts from 0, it always be 0 because you are multiplying it by 2. Its an infinite loop.
Related
This question already has an answer here:
What is the complexity of this function with nested loops?
(1 answer)
Closed 2 years ago.
The loop is
int count = 0;
for (int i = 0 ; i<n ; i++)
for (int j = 0 ; j<i ; j++)
count++;
I calculated the complexity = n * n(n+1)/2 so it will be n^3
but the answer is n^2 why?
You just need to check that j goes from 0 to i. As i goes from 0 to n, we have:
0 + 1 + 2 + ... + n =
1 + 2 + ... + n =
(n + 1) * (n / 2) = (n² + n)/2 = O(n²)
There is an extra n multiplying the answer in your calculation that comes from nowhere and this is the problem in your complexity.
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 3 years ago.
Improve this question
public int solution(int[] A) {
// A - array of bulbs. A[i] its position in row.
// return number of moments where all turned on bulbs are shined
// start from 0 to length-1, switch on bulbs ( A[i] represents a bulb's position)
// A[i] bulb shined if: 1) A[i] is switched 2) 1..A[i]-1 all are shined
// examples:
// input: {1,2,3,4,5} output: 5
// input: {1} output: 1
// input: {2,3,4,1,5} output: 2
// input: {2,1,3,5,4} output: 3
}
I suggested to iterate i: from 0 to a length-1, save every A[i] in a SortedSet. Check if there are i-1 elements in headSet < A[i]. If yes - we A[i] is shined.
It seems the performance of the solution above is low...
Can anybody suggest better?
You could do it in O(n):
public int solution(int[] a) {
Set<Integer> missing = new HashSet<>();
Set<Integer> store = new HashSet<>();
int count = 0;
for (int i = 0; i < a.length; i++) {
if (!store.contains(i + 1) && i + 1 != a[i])
missing.add(i + 1);
if (i + 1 < a[i])
store.add(a[i]);
else
missing.remove(a[i]);
if (missing.isEmpty())
count++;
}
return count;
}
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 7 years ago.
Improve this question
I am trying to see if I have these Big O questions right:
Determine the Big-O of the following:
a. for (i = 0; i < N; i++){
sequence of statements
}
for (j = 0; j < 1000000000*M; j++){
sequence of statements
}
This is O(NM) correct?
b. for (i = 0; i < N; i++) {
for (j = 0; j < i; j++) {
sequence of statements
}
}
for (k = 0; k < N; k++) {
sequence of statements
}
Is this O(n^4)?
c. for (i = 0; i < N; i++) {
for (j = i; j < i*i; j++) {
sequence of statements
}
I'm kinda stuck on this one....O(N^5)? or O(N^4) ?
a) No, first one is incorrect because both the for-loops are independent. The first for loop iterates for N times, whereas the second for-loop iterates for 1000000000*M times.
If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1 + f2 = O(|g1| + |g2|).
Check this Wikipedia link on Big O notation to know why the above.
So, overall time complexity = O(|N| + |M|).
b) The nested loop's time complexity comes out to be 1 + 2 + ... + N = N * (N+1)/2 = O(N2).
And, k-variable guided loop's complexity is O(N).
So, overall time complexity in this case is O(N2).
c) The 3rd case is somewhat complex.
When N = 2, the total iteration of both-loops = 0.
When N = 3, the total iteration of both-loops = 2.
When N = 4, the total iteration of both-loops = 2 + 6 = 8.
When N = 5, the total iteration of both-loops = 2 + 8 + 12 = 22.
...
When N = N(equals), the total iteration of both-loops = 2 + 8 + 22 + ... + (N-1)*(N-2) =
So, the total complexity
= 2 + 8 + 22 + ... + (N^2 - 3*N + 2)
= 1/3 * (N-2) * (N-1) * N
Check this link to know how it was derived
= O(N^3).
So, overall time complexity = O(N3).
This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 8 years ago.
int n = 500;
for(int i = 0; i < n; i++)
for(int j = 0; j < i; j++)
sum++;
My guess is this is simply a O(N^2), but the j < i is giving me doubts.
int n = 500;
for(int i = 0; i < n; i++)
for(int j = 0; j < i*i; j++)
sum++;
Seems like an O(N^3)
int n = 500;
for(int i = 0; i < n; i++)
for(int j = 0; j < i*i; j++)
if( j % i == 0 )
for( k = 0; k < j; k++ )
sum++
O(N^5)?
So for each loop j has a different value. If it was j < n*n, it would've been more straight forward, but this one is a tricky one, so please help. Thanks.
In the first case sum++ executes 0 + 1 + ... + n-1 times. If you apply arithmetic progression formula, you'll get n (n-1) / 2, which is O(n^2).
In the second case we'll have 0 + 1 + 4 + 9 + ... + (n-1)^2, which is sum of squares of first n-1 numbers, and there's a formula for it: (n-1) n (2n-1)
The last one is interesting. You can see, actually, that the most nested for loop is called only when j is a multiplicand of i, so you can rewrite the program as follows:
int n = 500;
for(int i = 0; i < n; i++) {
for(int m = 0; m < i; m++) {
int j = m * i;
for( k = 0; k < j; k++)
sum++
}
}
It's easier to work with math notation:
The formula is derived from the code by analysis: we can see that sum++ gets called j times in the innermost loop, which is called i times, which is called n times. In the end, the problem boils down to a sum of cubes of first n numbers plus lower-order terms (which do not affect the asymptotics)
One final note: it looks obvious, but I'd like to show that in general sum of first N natural numbers in dth power is Ω(N^(d+1)) (see Wikipedia for Big-Omega notation), that is it grows no slower than that function. You can apply the same reasoning to prove that a stronger condition is satisfied, namely, it belongs to Θ(N^(d+1)), which combines both Ω and O.
You are right for all but the last one, which has a tighter bound of O(n^4): note that the last for loop is only executed if j is a multiple of i. There are x / i multiples of i lower than or equal to x, and i * i / i = i. So the last loop is only executed for i values out of the i * i.
Note that big-oh gives an upper bound, so i*i vs n*n makes little difference. Strictly speaking, saying they are all O(n^2015) is also correct (because that is a valid upper bound), but it's hardly helpful, so in practice a tight bound is usually used.
IVlad already gave the correct answer.
I think what confuses you is the "Big Oh" definition.
N^2 has O(N^2)
1/2N^2 has O(N^2)
1/2N^2 + c*N + b also has
O(N^2) - by given c and b are constants independent from N
Check Big Oh definition from here
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
What is the worst-case complexity of the following algorithms, where the complexity of the {sequence of code} is O(1)? so I'm looking for worse case complexity of the code written here if the rest is O(1)...
a)1: for (i= 0; i < n; i++)
2: {sequence of code}
for this one I've got 0(n)
b) 1: int i = 0;
2: if (i > =0)
3: {sequence of code}
I got o(1)
the rest I don't understand.
c) 1: for (i = 0; i < n; i++ )
2: for (j = n; j >0; j--)
3: {sequence of code}
d) 1:for (i = n; i <n+10; i++ )
2: {sequence of code}
e) 1:for (i = 1; i <= n; i++)
2: {
3: for (j = 1; j <= n; j++)
4: {sequence of code}
5: for (k = 1; k <= 2n; k++)
6: {sequence of code}
7: }
Yes, O(n).
Yes, O(1).
The outer loop executes N times. The inner loop executes N times for each iteration of the outer loop.
The loop executes a constant number (10) of times...
The outer sequence of code is executed inside two nested loops, just about like in 3. The inner sequence of code is nested inside yet another loop, also executing N times for each iteration of the next loop.