1

I recently came across a function called the strawman algorithm which the pseudo code looks like this:

StrawmanSubarray(A):
  Initialize bestsum = A[0]  
  For left=0 to n-1:
     For right = left to n-1:
        Compute sum A[left] + . . . + A[right]
        If sum > bestsum: bestsum = sum

The time complexity is Θ (n^3), and I don't quite get where is the 3rd n comes from to get Θ (n^3)?

sctts-lol
  • 557
  • 5
  • 9

1 Answers1

1

well, you are iterating Θ (n^2) times. but, note the following: for each iteration, you are performing the following command: Compute sum A[left] + . . . + A[right] and this computation is Θ (n), so its Θ (n^2)*Θ (n) = Θ (n^3).

Ido
  • 92
  • 8