Time Complexity of the C program - c

What will be the time complexity of the following function?
is it O(n^3) or O(n^4)?
i am getting O(n^3)
in the first for loop, it will undergo n times.
in the second forloop, for every nth element it will go n^2 times, therefore the total complexity till here is O(n^3)
now, the if statement will only hold true value only for n out of n^2 values, and for every n values the k- for loop will go till n^2 elements and hence the complexity is O(n^3).
I have taken few values of n:
for n=3 ,c=25
for n=10,c=1705
for n=50,c=834275
for(i=1;i<=n;++i)
for(j=1;j<=(i*i);++j)
if((j%i)==0)
for(k=1;k<=j;++k)
c=c+1;

Time complexity of such program is O(n^3) magnitude.

Related

what would be the time complexity of this algorithm?

i was wondering what would be the time complexity of this piece of code?
last = 0
ans = 0
array = [1,2,3,3,3,4,5,6]
for number in array:
if number != last then: ans++;
last = number
return ans
im thinking O(n^2) as we look at all the array elements twice, once in executing the for loop and then another time when comparing the two subsequent values, but I am not sure if my guess is correct.
While processing each array element, you just make one comparison, based on which you update ans and last. The complexity of the algorithm stands at O(n), and not O(n^2).
The answer is actually O(1) for this case, and I will explain why after explaining why a similar algorithm would be O(n) and not O(n^2).
Take a look at the following example:
def do_something(array):
for number in array:
if number != last then: ans++;
last = number
return ans
We go through each item in the array once, and do two operations with it.
The rule for time complexity is you take the largest component and remove a factor.
if we actually wanted to calculate the exact number of operaitons, you might try something like:
for number in array:
if number != last then: ans++; # n times
last = number # n times
return ans # 1 time
# total number of instructions = 2 * n + 1
Now, Python is a high level language so some of these operations are actually multiple operations put together, so that instruction count is not accurate. Instead, when discussing complexity we just take the largest contributing term (2 * n) and remove the coefficient to get (n). big-O is used when discussing worst case, so we call this O(n).
I think your confused because the algorithm you provided looks at two numbers at a time. the distinction you need to understand is that your code only "looks at 2 numbers at a time, once for each item in the array". It does not look at every possible pair of numbers in the array. Even if your code looked at half of the number of possible pairs, this would still be O(n^2) because the 1/2 term would be excluded.
Consider this code that does, here is an example of an O(n^2) algorithm.
for n1 in array:
for n2 in array:
print(n1 + n2)
In this example, we are looking at each pair of numbers. How many pairs are there? There are n^2 pairs of numbers. Contrast this with your question, we look at each number individually, and compare with last. How many pairs of number and last are there? At worst, 2 * n, which we call O(n).
I hope this clears up why this would be O(n) and not O(n^2). However, as I said at the beginning of my answer this is actually O(1). This is because the length of the array is specifically 8, and not some arbitrary length n. Every time you execute this code it will take the same amount of time, it doesn't vary with anything and so there is no n. n in my example was the length of the array, but there is no such length term provided in your example.

Time complexity of an algorithm that consists a function in disguise

Def Function1(arr):
for i in range(len(arr)):
If function2(arr,arr[i]):
a+arr[i]
return a
Def Function2(arr,i):
for i in range(len(arr)):
If arr[i]==i:
return True
return False
is the time complexity of this algorithm n^2 as the for loop for Function1 executes for n times and in each iteration function2 is executed when the if statements executes where another for loop runs for n times in the worst case. Hence n*n=n^2
Your first loop iterates |arr| times (meaning the length of arr), and the second one |A| times. The time complexity would therefore be O(|arr| * |A|).
If these arrays have the same length n, then yes it would be O(n2). If they depend on each other in some other manner, you can also simplify and express it in terms of one variable n. If you have no such information, then the best you can do is to express the time complexity using both variables.

Time complexity of linear search in a loop?

For I=1 to n
For J=1 to n
k = b[I]
F = Linear_search(a,k)
Print F
J=J*2
What will be the time complexity of above algorithm? I thought it would be O(nlogn) but there is a linear search too in algorithm which has complexity O(n).So what will be the complexity O(nlogn) or O(n) or will it be O(n^2 logn)?
There are :
n iterations for the first loop
log(n) iterations for the second
The program will call Linear_search nlog(n) times.
The complexity of the linear search being O(n), the complexity of the program will then be O(n^2log(n))

What is the time complexity of this algorithm which retrieves values from a 2D array?

This is the algorithm to retrieve values from a 2D array, what will be the time complexity of this algorithm?
for(i=0;i<n;i++){
for(j=0;j<2;j++){
x = arr[i][j]
}
}
So the time complexity of this algorithm would be O(2n) or O(n^2)? What is the reason?
Outer loop is iterating n times, but inner loop is iterating only two times, So time complexity will be O(2n) and not O(n^2). And
O(2n) = O(n)

time complexities on while loop with condition of n*n

i=1;
while(i<n*n)
i=i+n;
from my lecturer provided answer:
Big-O notation was O(n) instead O(n^2) why?
Because after each loop run n is added to i. So it has to run maximal n times to reach n², thus ending the loop.
O(n^2) would be:
i=1;
while(i<n*n)
i=i+1;

Resources