fork(1) download
  1. //In problem 7, time complexity would be O(n^4):
  2. //Proof:
  3. void function(int n)
  4. {
  5. int count = 0;
  6.  
  7. // executes n times
  8. for (int i=0; i<n; i++)
  9.  
  10. // executes O(i*i) times for all i
  11. for (int j=i; j< i*i; j++)
  12. if (j%i == 0)
  13. {
  14. /* executes j times when j%i == 0, which implies “total” # of iterations for each i would be
  15. i + 2*i + 3*i + ….. + (i - 1) * i
  16. = i*(1 + 2 + 3 + … + (i - 1))
  17. = i*(i - 1)*(i)/2 = O(i^3)
  18. The outer j loop iterates O(i * i - i + 1) times without the if condition and i - 1 times with the if condition
  19. =>total complexity of j loop when inner loop is taken into consideration is
  20. O(i*i - i + 1) + O((i - 1)*(i - 1)*(i - 1)) = O(i*i*i)
  21. When outer i loop is also considered, the complexity becomes
  22. Sum of i*i*i for all I = 1, 2, 3, 4, …., n
  23. = O(n^4)*/
  24. for (int k=0; k<j; k++)
  25. printf("*");
  26. }
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void function(int)’:
prog.cpp:25:31: error: ‘printf’ was not declared in this scope
                     printf("*");
                               ^
stdout
Standard output is empty