Help with my big oh algorithm analaysis

  • Thread starter Thread starter mycrafish
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread Summary
The code snippet involves nested loops that contribute to the overall time complexity analysis. The outer two loops run n^2 times, while the inner loops vary based on conditions. The first inner loop executes n*n times when i equals j, and the second inner loop runs log2(n) times for the else condition. The overall complexity is derived as O(n^3 + n^2 log2(n) - n log2(n)). Understanding the contribution of each loop and condition is crucial for accurate big O analysis.
mycrafish
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if (i == j){
for(int k = 0; k < n*n; k++){
System.out.println();
}
}
else{
for(int h = 1; h <= n; h = h*2){
System.out.println();
}
}
}​
 
Physics news on Phys.org
Any attempt at a solution? Where have you gotten so far? As a small hint, O(n) is the worst case, so look at those loops and think where the most steps can be taken.
 
i know the answer is n3 + n2log2n - nlog2n but i have no idea how to get this far
I know the loops runs n^2 times at least but I am not sure how to do the if statements
 

Similar threads

Replies
7
Views
2K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
8
Views
2K
Replies
1
Views
2K
Back
Top