I am trying to understand the time complexity of this algorithm, anyone who can let me know of the runtime complexity descriptively not asymptotically, the code;
array = [-3, 0, 1, 2, -1, 1, -2]
def triplets_sum(nums):
n = len(nums)
lst = []
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
if nums[i] + nums[j] + nums[k] == 0 and sorted([nums[i], nums[j], nums[k]]) not in lst:
lst.append(sorted([nums[i], nums[j], nums[k]]))
return lst
triplets_sum(array)
O(n^3)
Each
iwould be run n*(n+1)/2 times which gives Triangular numbers. Well, to be more exact, it would be run (n-2)*(n-1)/2, but that's the same O notation.If you start with n=3, you get 1 run. Every time n goes up, you get what you had before, plus (n-2).
But, of course what you need is the sum of all these, that's called triangular pyramidal and it's n*(n+1)*(n+2)/6 which is O(n^3).
Sorry my last answer incorrectly didn't sum over
iand so under counted.Update with additional explanation
A good place to start is with small
ncases. For example, atn = 3, the loop runs once, sinceiis 1~3,jis 2~3 andkis only 3.When we move to
n = 4, thenjis 2~4 andkis 3~4. On thej = 2run,kruns twice, and on thej = 3run,kruns only once. That's 3 runs total.Of course, we can't keep running through all the possibility like this, so I made an excel sheet. For any given
n, whati,j, andkvalues occur.First, start with an
n. They are in green. For anyn, whativalues are possible? They are listed underiso that anyithat's right of annwill happen. For example, atn = 7, the possible values foriare[7, 6, 5, 4, 3]. Ok, so now we know whichivalues will happen, next whatjvalues will happen for any giveni? I decided to be verbose here. For everyi, I listed all the possiblejvalues next to it. Let's say we're looking atn = 7still. One possibleivalue in that case is 5. Wheni = 5, then we can see the possiblejvalues are[4, 3, 2]. Ok, getting close now. For each of thosejvalues, how many times willkrun? Well,kwill always run one fewer time thanj, so in thekcolumn, this is the value I wrote.With all this is written out, now it's time to work backwards and summing up as we go. Let's zero in on
n = 7,i = 5,j = 3,k = 1 or 2, I marked that cell in blue. There are 2 possible values forkat thisjvalue. For thisi = 5value, there are 3 possiblejvalues, namely 4, 3, and 2. Summing up the timeskruns for each of these, we get3 + 2 + 1 = 6which we see in thei totalcolumn. The last step is to sum up all theivalues that happen for any givenn. Ifn = 7, then we have to sum up all theivalues in[7, 6, 5, 4, 3]which is15 + 10 + 6 + 3 + 1or35.The final step is finding a general solution. We can see that to find
i totalwe are summing up all integers from 1 to (i-2). Then to find then totalwe are summing up thei totals. Well, summing up all integers from 1 to x gives Triangular numbers, as I mentioned before, and summing up Triangular numbers gives Triangular pyramidal numbers. The formula for that isn*(n+1)*(n+2)/6. In this case, we start fromn = 3so this particular case would be(n-2)*(n-1)*(n)/6. Plugging in somenvalues to this matches our hand-derived values. With O notation, we can basically strip out constants, so it would ben * n * nor O(n^3).This is basically representing a 4D array in excel, so I understand it's not clear at first glance, but if you step through, I think you'll find each step stands. I'm open to corrections or better explanations.