What will be the time complexity of the following program

DoomLord

Can anyone find the time complexity of the following python 3.6 program :

I tried to find and my answer is O(N*n), where N is the range of the very first loop and n is the range of the second for loop which is inside the first for loop

This program is for https://www.codechef.com/FEB20B/problems/SNUG_FIT problem on codechef.

N=int(input())
for _ in range(N):
    S=0
    n=int(input())
    A=list(map(int, input().split()))[:n] #Assume amount of inputs to be strictly of n numbers
    B=list(map(int, input().split()))[:n]
    A.sort();A.reverse()
    B.sort();B.reverse()

    for i in range(0,len(A)): # Here range is n (same as len(A))
        if A[i]>=B[i]:
            S=S+B[i]
        else:
            S=S+A[i]
    print(S)
Faruk Hossain

@Pritam Banerjee wrote -

O(N( nlogn + n)) which effectively is O(Nlog(n))

Actually it is not true.

I am describing it more clearly along with the code.

N=int(input())
for _ in range(N):    # O(N)
    S=0
    n=int(input())
    # The following line is O(n)
    A=list(map(int, input().split()))[:n] #Assume amount of inputs to be strictly of n numbers
    # This line is also O(n)
    B=list(map(int, input().split()))[:n]
    A.sort();A.reverse()    # this is O(nlog(n))
    B.sort();B.reverse()    # this is also O(nlog(n))

    for i in range(0,len(A)): # Here range is n (same as len(A))  # this is O(n)
        if A[i]>=B[i]:
            S=S+B[i]
        else:
            S=S+A[i]
    print(S)

So overall O(N( n + n + nlog(n) + nlog(n) + n)

We can calculate in the following way -

  O(N( n + n + nlog(n) + nlog(n) + n)
= O(N( 3n + 2nlog(n))
= O(N (n + nlog(n)) (we can eliminate const number when determining complexity)
= O(N(nlog(n)) (as n > nlog(n), so we can eliminate the small part)
= O(N*nlog(n))

So overall complexity is O(N*nlog(n), not O(Nlog(n).

There are more difference between O(N*nlog(n) and O(Nlog(n).

Hope this post will help you a lot.

Thanks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is time complexity of following?

what is the time complexity of this program

What is the time complexity of the following function?

What is the time complexity of the following equation

What is the time complexity of the following pseudocode?

What will be the time complexity of following code?

What will be the time complexity of the following code?

Is time complexity of following program is O(1)?

What is the Time Complexity of this JAVA program

what is the time complexity of this pascal program?

What is the time complexity of my program?

What should be the time complexity of the program

what will be the time complexity of the following program for fibonacci series? code uses dynamic programming

What is the time complexity of the following nested loop dependency?

What is the time complexity of the following recursive code

What is the time complexity of the following search algorithm?

What is the time complexity of following nested dependent loops

What will be the time complexity of following recursive algorithm?

What is the time complexity of the following code segment?

What should be the time complexity of the following loops?

What is the time complexity of the following piece of code

What time complexity does the following code have?

What is the time complexity of the following python codes

what is the correct time complexity for this following code?

What's the time complexity of below program?

Can someone help me find the time complexity of the following program?

Time complexity of the following algorithm?

time complexity for following function

What causes run time error in the following program?