I am trying to find the running time of the following loop:
int m=1;
for(i=1;i<=k;i++)
{
for(j=1;j<=A[i];j++)
{
B[m]=i;
m++;
}
}
Here, A is an array keeping integers in it, and the sum of these integers is n. For example, if the length of A is 2, and A[1]=2 and a[2]=4, then inner loop will run 6 times.
So, in my lecture notes, it says the running time of this piece of code is O(n+k). But assume, for example, that k is 5, and length of array A is 4, and A[1]=3, A[2]=0, A[3]=0, A[4]=9, A[5]=0. So, n=12. Then, for k=1, inner loop will iterate 2 times, for k=2 the outer loop will iterate 1 time, for k=3 the outer loop will run 1 time, for k=4 the inner loop will run 9 times and for k=5 the outer loop will run 1 time, so total of iterations is 14. This is greater than both k and n, and running time is neither O(n) nor O(k). What am i doing wrong here?
Thank you