Skip to content

Instantly share code, notes, and snippets.

@richardoey
Last active March 18, 2019 12:33
Show Gist options
  • Save richardoey/171d76d06ee15bc03fb252a3ebd9330a to your computer and use it in GitHub Desktop.
Save richardoey/171d76d06ee15bc03fb252a3ebd9330a to your computer and use it in GitHub Desktop.
Question 2 (What is the computational complexity of your answer in Question 1? Can you explain why?)

Answer 2
What is the computational complexity of your answer in Question 1? Can you explain why?

The computational complexity of Question 1 is O(n2).

Explanation:

for i in range(len(arr2)):
    for j in range(len(arr1)):
        if(arr2[i].strip().upper() == arr1[j].strip().upper()):
            count+=1
            break

We need to check whether the array2 is the subset of array1 or not by doing the iteration in array2 and array1, with 'n' iterations. Moreover, we assume that the number of the lengths of array2 and array1 is the same ( 'n' numbers )

First we need to do 'n' times iteration (length arr2) and check to each member in array1 (length arr1), so it means we will iterate with 'm' times (I denotes the length of array1 with 'm'). Eventhough I'm using 'break' after it's found that the member of array2 is the subset of array1, the maximum possibilities this algorithm will be worked still n x m times.

Therefore, the complexity of my answer in answer1.py will be O(n2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment