Question: How do I calculate the time complexity of the following program?

```
int[] vars = { 2, 4, 5, 6 };
int len = vars.length;
int[] result = new int[len];

for (int i = 0; i < len; i++) {
    int value = 1;

    for (int k = 0; k < i; k++) {
        value = value * vars[k];
    }
    for (int j = i + 1; j < len; j++) {
        value = value * vars[j];
    }

    result[i] = value;
}

```

and how is the above one same as below?

```
for (int i = 0; i < len; i++) {
    int value = 1;

    for (int j = 0; j < len; j++) {
        if(j != i) {
            value = value * vars[j];
        }
    }

    result[i] = value;
}

```

Answer: 
**For the first fragment:**

![enter image description here](https://i.stack.imgur.com/849dn.png)

**For the second fragment:**

![enter image description here](https://i.stack.imgur.com/gDA1q.png)
A general approach in determining the complexity is counting the iterations.
In your example, you have an outer for loop with two loops nested in it. Note: Instead of len, I'll write n.
The outer loop

```
for (int i = 0; i < n; i++)

```

iterates `n`-times.
The number of iterations of the two next loops are actually more easy to count, than it looks like:
The second loop iterates `i`-times and the third `n-i`-times. If you add them together you get `n`-many iterations within the outer loop.

Finally, if the outer loop does `n` iterations and within each of these iteration the code loops another `n` times you get the result of `n^2` iterations. In the traditional notation of complexity-theory you'd write, that the algorithm has an upper-bound of `n^2` or is in `O(n)`.