No Idea!

  • + 18 comments

    The time out error was because you weren't explicitly making A and B as sets.The inputs have duplicate values in A and B and we need to remove them. My code was also facing the same problem but now it's fixed.Have a look at this-

    n,m=map(int,input().split())
    l=input().split(' ')
    A=set(input().split(' '))
    B=set(input().split(' '))
    happiness=0
    
    for i in l:
        if i in A:
            happiness+=1
        if i in B:
            happiness-=1
    print(happiness)
    

    I agree that it's complexity is O(n) which is greater than O(m) and it takes one temperory variable.