2,793 views
1 1 vote

What is the best case and worst case of the algorithm? And when will best case and worst case will happen??

int main()
{
   for(i=1 ; i<=n ; i++)
   {
      if(n%i == 0)
      {
         for(j=1 ; j<=n ; j++)
         { 
            printf("Hello");
         }
      }
  }
}

1 Answer

0 0 votes

Firstly its not mentioned in the question if “n” is even or odd. So assuming in best case its only divisible by 1 and then time complexity becomes Ω(n)  but in worst case “n” is divisible by all numbers making the worse case time as O(n^2)

Position:
Show:

Related questions

0 0 votes
1 1 answer
1.7k
1.7k views
tusharb asked Feb 18, 2022
1,664 views
As we know the time complexity of solving the greedy knapsack algorithm depends mainly on the sorting algorithm used, Can we use counting sort as the sorting algorithm to...
0 0 votes
1 1 answer
311
311 views
eyeamgj asked Oct 16, 2018
311 views
https://gateoverflow.in/154097/find-the-value-of-p AND THE TIME COMPLEXITY WILL BE O(nlogn)??
1 1 vote
2 answers 2 answers
619
619 views
vishal_prasad_singh asked Nov 5, 2025
619 views
O(n)∗Ω(n)+Θ(n) = ?1.O(n)2.Ω(n)3.Θ(n)4. none 
0 0 votes
1 1 answer
470
470 views
Ayushhh_ asked Jul 15, 2024
470 views
What is the time complexity of the following functionint i = n;while(i>=n){i = i-30;i=i+15;i=i+25;}