Skip to content

Problems

Course: Arrays - chapter 4

Q: Analyse the complexity for the following code

for(let i=0; i<k; i++) {
for(let j=0; j<k; j++) {
console.log(i + " " + j);
}
}
Solution

First, we need to generate a function f(n) that returns the time taken to execute for any input n.

For a given n, the outer loop of i runs for 0 to n-1.

For each value of i, the inner loop runs again for 0 to n-1

So for each i, the inner loop runs n times.

Total complexity = n + n + n + …. + n (n times as i runs for n times)

f(n) = n ^ 2

So T.C = O(k^2)

Q: Analyse the complexity for the following code

for(let i=0; i<k; i++) {
for(let j=0; j<=i; j++) {
console.log(i + " " + j);
}
}
Solution

For a given i, j runs from 0 to i

For i = 0: j only runs 1 time

For i = 1: j runs 2 times

For i = 2: j runs 3 times

For i = 3: j runs 4 times

..

For any general i = k: j runs for 0, 1, 2, …, k so k times

So total complexity

1 + 2 + 3 + …. + n = ⁍

Complexity: O(k^2)

Q:

for(let i=1; i<n; i *= 2) {
console.log(i);
}
Solution

Let’s see how i runs

i = 1, 2, 4, 8, 16, …, x

where x is the highest power of 2 less than n. Let’s say x = ⁍, then ⁍

So how many steps?

Step 0: i = 2 ^ 0

Step 1: i = 2 ^ 1

Step 2: i = 2 ^ 2

Step 3: i = 2 ^ 3

..

Step p: i = 2 ^ p = x

But what is p?

⁍

or p ≤ ⁍

Complexity: O(⁍

Q:

for(let i=1; i<n; i++) {
for(let j=1; j<n; j+=i) {
console.log(i + " " + j);
}
}
Solution

For i = 1, j runs n times (as it is same as j += 1)

For i = 2, j runs n/2 times (as j += 2)

For i = 3, j runs n/3 times

For general i = k, j runs n / k times

n + n/2 + n/3 + n/4 + …. + n/k +.. + 1

n (1 + 1/2 + 1/3 + … 1/n)

Now we need to find the complexity of:

⁍