-2

Consider:

bar (a) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < i; ++j)
      a = a * (i + j);

  return a;
}

Find the time complexity for the above function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tayyab
  • 1
  • This is an ***exact*** duplicate of *[How can I find the time complexity of an algorithm?](https://stackoverflow.com/questions/11032015/how-can-i-find-the-time-complexity-of-an-algorithm/33474486#33474486)* as [one of the answers](https://stackoverflow.com/questions/11032015/how-can-i-find-the-time-complexity-of-an-algorithm/33474486#33474486) covers it. Or in other words, the answer could have been found by reading that answer. – Peter Mortensen Apr 06 '22 at 14:45
  • Does this answer your question? [How can I find the time complexity of an algorithm?](https://stackoverflow.com/questions/11032015/how-can-i-find-the-time-complexity-of-an-algorithm) – Peter Mortensen Apr 06 '22 at 14:46

1 Answers1

0

The time complexity is O(n^2). Since the inner for loop runs from 0 to i and outer for loop runs from 0 to n it follows the pattern 1 + 2 + ... + n and this sums up to n*(n-1)/2, which in turn is O(n^2)

Adeeb HS
  • 139
  • 7