Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need Big O rating for the 4 groups of code if (n < 100) System.out.println (\"n:

ID: 3546669 • Letter: N

Question

Need Big O rating for the 4 groups of code

if   (n < 100)

    System.out.println ("n: "  + n); O(1)

for   (int x = 0; x < n % 2; x++)
      System.out.println ("x: "   + x);

for   (int x = 0; x < n; x++)
        for (int j = 0; j < m; j++)
          System.out.println ("x:   " + x);  O(n^2)

for (int x = 0; x < n; x++)
      for (int j = 0; j < n; j++)
          if (x < 0)

           for (int k = 0; k < n % 10; k++)
                 System.out.println   ("x: " + x);

  

if   (n < 100)

  

    System.out.println ("n: "  + n); O(1)

     

for   (int x = 0; x < n % 2; x++)
      System.out.println ("x: "   + x);

     

for   (int x = 0; x < n; x++)
        for (int j = 0; j < m; j++)
          System.out.println ("x:   " + x);  O(n^2)

     

for (int x = 0; x < n; x++)
      for (int j = 0; j < n; j++)
          if (x < 0)

  

           for (int k = 0; k < n % 10; k++)
                 System.out.println   ("x: " + x);

  

Explanation / Answer


if (n < 100)

System.out.println ("n: " + n);                 O(1) as it is in one single statement and execute once so it take constant time



for (int x = 0; x < n % 2; x++)
System.out.println ("x: " + x);                O(n) as it run until x < n%2 which can run some n/constant time but on removing

                                                           the constant coofficent it running time in O(n)



for (int x = 0; x < n; x++)
for (int j = 0; j < m; j++)
System.out.println ("x: " + x);                O(n^2) as first for loop run second for loop n time and the second for loop run n time

and the    System.out.println ("x: " + x);      run n^2 hence it run O(n^2)


for (int x = 0; x < n; x++)
for (int j = 0; j < n; j++)
if (x < 0)

for (int k = 0; k < n % 10; k++)
System.out.println ("x: " + x);    O(n^3)
as first for loop run second for loop n time and the second for loop run n time

                                                                  third for loop n time System.out.println ("x: " + x);      run n^3 hence it run

                                                                  O(n^3)












Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote