1,263 views
1 votes
1 votes

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 votes
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)

Related questions

0 votes
0 votes
1 answer
1
tusharb asked Feb 18, 2022
654 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 votes
0 votes
1 answer
2
eyeamgj asked Oct 16, 2018
131 views
https://gateoverflow.in/154097/find-the-value-of-p AND THE TIME COMPLEXITY WILL BE O(nlogn)??
0 votes
0 votes
3 answers
3
Nisha Bharti asked Sep 26, 2022
675 views
What is the time & space complexity of this algorithm?Main(){ for(i=n; i>10; i=i^1/4) { for(j=201; j<n^3; j=j+400) ...
0 votes
0 votes
1 answer
4
eyeamgj asked Jun 24, 2018
397 views
consider the following c program A(n){if(n<=1)return(n2 +n+1)elsereturn(5A(n/2)+3A(n/2)+MA(n))}where MA(n) has complexity O(n2).1.what is the recurrence relation for valu...