What is the Big-O running time for this code? Explainyour answer. for (int i=0;
ID: 3616404 • Letter: W
Question
What is the Big-O running time for this code? Explainyour answer.for (int i=0; i<num; i++) System.out.println(i+1);
What is the Big-O running time for this code? Explain your answer. for (int i=0; i<num; i++) for (int j=0; j<num2; j++) System.out.println((i+1) * (j+1) ); What is the Big-O running time for this code? Explainyour answer.
for (int i=0; i<num; i++) System.out.println(i+1);
What is the Big-O running time for this code? Explain your answer. for (int i=0; i<num; i++) for (int j=0; j<num2; j++) System.out.println((i+1) * (j+1) );
Explanation / Answer
for (int i=0; i<num; i++) System.out.println(i+1);The for loop runs "num" times
suppose num= n, i.e. the for loop runs n times
Therefore the complexity of the given code snippet isO(n)
b) for (int i=0; i<num; i++) for (int j=0; j<num2; j++) System.out.println((i+1) * (j+1) );
The outer for loop runs "num" times
For every iteration of the outer for loop...the inner for loop runs"num2" times.
Therefore the total number of times the third line of the codesnippet runs is "num * num2" times
let n = maximum( num, num2)
Therefore the total number of times the third statement is executedis < n* n
< n2
Therefore the complexity of the given code snippet isO(n2)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.