//In problem 7, time complexity would be O(n^4):
//Proof:
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i<n; i++)
// executes O(i*i) times for all i
for (int j=i; j< i*i; j++)
if (j%i == 0)
{
/* executes j times when j%i == 0, which implies “total” # of iterations for each i would be
i + 2*i + 3*i + ….. + (i - 1) * i
= i*(1 + 2 + 3 + … + (i - 1))
= i*(i - 1)*(i)/2 = O(i^3)
The outer j loop iterates O(i * i - i + 1) times without the if condition and i - 1 times with the if condition
=>total complexity of j loop when inner loop is taken into consideration is
O(i*i - i + 1) + O((i - 1)*(i - 1)*(i - 1)) = O(i*i*i)
When outer i loop is also considered, the complexity becomes
Sum of i*i*i for all I = 1, 2, 3, 4, …., n
= O(n^4)*/
for (int k=0; k<j; k++)
printf("*");
}
}
Ly9JbiBwcm9ibGVtIDcsIHRpbWUgY29tcGxleGl0eSB3b3VsZCBiZSBPKG5eNCk6Ci8vUHJvb2Y6CnZvaWQgZnVuY3Rpb24oaW50IG4pCnsKICAgIGludCBjb3VudCA9IDA7CiAKICAgIC8vIGV4ZWN1dGVzIG4gdGltZXMKICAgIGZvciAoaW50IGk9MDsgaTxuOyBpKyspCiAKICAgICAgICAvLyBleGVjdXRlcyBPKGkqaSkgdGltZXMgZm9yIGFsbCBpCiAgICAgICAgZm9yIChpbnQgaj1pOyBqPCBpKmk7IGorKykKICAgICAgICAgICAgaWYgKGolaSA9PSAwKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAvKiBleGVjdXRlcyBqIHRpbWVzIHdoZW4gaiVpID09IDAsIHdoaWNoIGltcGxpZXMg4oCcdG90YWzigJ0gIyBvZiBpdGVyYXRpb25zIGZvciBlYWNoIGkgd291bGQgYmUKaSArIDIqaSArIDMqaSArIOKApi4uICsgKGkgLSAxKSAqIGkgCj0gaSooMSArIDIgKyAzICsg4oCmICsgKGkgLSAxKSkKPSBpKihpIC0gMSkqKGkpLzIgPSBPKGleMykKVGhlIG91dGVyIGogbG9vcCBpdGVyYXRlcyBPK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